Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"expected string or buffer"

Tags:

python

string

I have this code:

x=os.system("host www.google.com")
b=re.findall(r'\w',x)
print b  

But this returns the following error:

TypeError: expected string or buffer  
like image 795
Mudasar Yasin Avatar asked Aug 30 '26 09:08

Mudasar Yasin


1 Answers

The return value from os.system is the exit code of the process. This is an integer, not a string, so you're basically doing this:

>>> re.findall(r'\w', 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 177, in findall
    return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer

I think the function you're looking for is subprocess.check_output:

>>> import subprocess
>>> print subprocess.check_output(['host',  'www.google.com'])
www.google.com has address 173.194.75.147
www.google.com has address 173.194.75.99
www.google.com has address 173.194.75.103
www.google.com has address 173.194.75.104
www.google.com has address 173.194.75.105
www.google.com has address 173.194.75.106
www.google.com has IPv6 address 2607:f8b0:400c:c03::6a
like image 143
jterrace Avatar answered Aug 31 '26 22:08

jterrace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!