0

I'm new to the python programming language and I encountered a problem while doing something (apparently not) fairly simple. This is the code :

# Get the list of available network interfaces
listNIC = os.system("ifconfig -s | awk '{print 1ドル}'")
listNIC.split('\r?\n')
# Get the name of the wireless network card with iwconfig
wlanNIC = ''
i = 0
while i < len(listNIC) :
 if listNIC[i].match('eth[0-9]{1}') :
 wlanNIC = listNIC[i]
 break
 i += 1

First error comes at line 3, because for some odd reason listNIC is of type int. The error is :

Traceback (most recent call last):
 File "Kol.py", line 9, in <module>
 listNIC.split('\r?\n')
AttributeError: 'int' object has no attribute 'split'

I solved it by changing :

listNIC = os.system("ifconfig -s | awk '{print 1ドル}'")

into

listNIC = str(os.system("ifconfig -s | awk '{print 1ドル}'"))

But now I get an even stranger problem. I get an error that says that a string doesn't have an attribute match. Here's the error :

Traceback (most recent call last):
 File "Kol.py", line 15, in <module>
 if listNIC[i].match('eth[0-9]{1}') :
AttributeError: 'str' object has no attribute 'match'

So my question is the following :

  • How to solve the AttributeErrors and where do they come from ?

Thanks in advance !

asked Sep 22, 2012 at 21:11
2
  • 1
    Did you try printing the result of os.system("ifconfig -s | awk '{print 1ドル}'")? I guess this returns an integer (exit value) and not the output of the command. Commented Sep 22, 2012 at 21:15
  • And what makes you think a string should have an attribute match? That's an attribute of a regex object, not a string. Commented Sep 22, 2012 at 21:16

1 Answer 1

5

os.system returns the exit code of the command, not its output. You turn this number into a string, but this will not do what you want it to do. It is also deprecated. You might want to look at the subprocess module.

output = subprocess.check_output('command', shell=True)

Furthermore, you need to match using the module re. Check its documentation for the precise syntax, but it should look something like re.match(your_pattern, yourstring).

Finally, although your version is not wrong, it is more common to loop through a list like in the sample below. It is slightly shorter and more readable as you save a variable and a call to len. It is also considered more pythonic.

for nic in listNIC:
 if re.match(pattern, nic):
 wlanNIC = nic
 break
answered Sep 22, 2012 at 21:14
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, ok I misunderstood the meaning of that method then. Thanks for your quick answer.
Thank you so much for the precisions ! Coming from JavaScript / Node.js I tend to apply it's syntax to python which produces terrible code ...

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.