0

I have the following code in Python 2.7 and am receiving the following error.

import os,subprocess,re
f = open("/var/tmp/disks_out", "w")
proc = subprocess.Popen(
    ["df", "-h"],
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)
out, err = proc.communicate()
for line in out:
    f.write(line)
f.close()
f1 = open("/var/tmp/disks_out","r")
disks = []
for line in f1:
    m = re.search(r"(c\dt\d.{19})",line)
    if m:
        disk = m.group[1]
        disks.append(disk)
print(disks)

Error:

disk = m.group[1]
TypeError: 'builtin_function_or_method' object is unsubscriptable

Does anyone know why this is happening?

2
  • 1
    Indentation matters in Python. Badly indented Python code is not a minimal reproducible example. If we guess how it is supposed to be indented, we might miss problems that we can't see. Please edit your question to accurately depict the indentation of your code. Commented Jul 23, 2018 at 14:05
  • ... but you probably meant m.group(1) instead of m.group[1] . Commented Jul 23, 2018 at 14:07

1 Answer 1

1

What you are doing is -

m.group[1]

But rather it should be -

m.group(1)

Look here

Example from docs -

>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0)       # The entire match
'Isaac Newton'
>>> m.group(1)       # The first parenthesized subgroup.
'Isaac'
>>> m.group(2)       # The second parenthesized subgroup.
'Newton'
>>> m.group(1, 2)    # Multiple arguments give us a tuple.
('Isaac', 'Newton')

What you are doing instead is-

disk=m.group[1]
# Trying to slice a builtin method, in this case, you are trying to slice group()
# and hence
TypeError: 'builtin_function_or_method' object is unsubscriptable

Square brackets [] are the subscript operator. If you try to apply the subscript operator to an object that does not support it, you get the error.

Sign up to request clarification or add additional context in comments.

1 Comment

I am able to get list show below.But i want to remove last two character from list (574) # ./f4.py ['c0t5000CCA025A29894d0s0']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.