使用 ubuntu 16.04 我想要的是使用 python 检索终端上的输出,我确实引用了这两个链接: How can I get terminal output in python?
Running shell command from Python and capturing the output
但是作为 python 的初学者我无法让它工作,我想要输出的原始代码:
for element in my_images:
os.system('you-get -o videos ' + element)
我的代码是如何变成的:
for element in my_images:
# value = os.system('you-get -o videos ' + element)
output = subprocess.Popen(os.system('you-get -o videos ' + element), stdout=subprocess.PIPE).communicate()[0]
print(output)
但是它没有用,我得到了这个错误
Traceback (most recent call last):
File "main_twitter.py", line 29, in <module>
output = subprocess.Popen(os.system('you-get -o videos ' + element), stdout=subprocess.PIPE).communicate()[0]
File "/usr/local/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/usr/local/lib/python3.6/subprocess.py", line 1220, in _execute_child
args = list(args)
TypeError: 'int' object is not iterable
请您参考如下方法:
您应该直接使用命令和参数调用Popen 的构造函数。调用 os.system 将执行命令并返回退出代码,这不是您要传递给 Popen 的内容:
output = subprocess.Popen('you-get -o videos ' + element, shell=True, stdout=subprocess.PIPE).communicate()[0]
