如何使用python检索命令行上显示的输出?

时间:2018-09-12 08:12:51

标签: python python-3.x shell subprocess

使用ubuntu 16.04我想要的是使用python在终端上检索输出,我确实引用了以下2个链接: 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

0 个答案:

没有答案