[英]“expected string or buffer”
我有這個代碼:
x=os.system("host www.google.com")
b=re.findall(r'\w',x)
print b
但是這會返回以下錯誤:
TypeError: expected string or buffer
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.