2012-09-27 134 views
3

我有这样的代码:“预期字符串或缓冲区”

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

但这返回以下错误:

TypeError: expected string or buffer 
+1

给我们一个关于环境提示您正在使用。 –

+1

完整堆栈跟踪。 http://sscce.org/和http://v.gd/whathaveyoutried – Marcin

回答

5

os.system的返回值是进程的退出代码。这是一个整数,而不是字符串,所以你基本上是这样做的:

>>> 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 

我认为你正在寻找的功能是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 
+0

刚刚编辑之前你评论:) – jterrace

+0

再次忍者! :^) – DSM

相关问题