How can i retrieve the output shown on my command line using python?
使用ubuntu 16.04我想要的是使用python检索终端上的输出,我确实参考了这两个链接:
如何在python中获得终端输出?
从Python运行shell命令并捕获输出
但作为python中的首发我无法使它工作,我想要输出的原始代码:
1 2 | for element in my_images: os.system('you-get -o videos ' + element) |
我的代码如何变成:
1 2 3 4 | 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) |
但它不起作用我得到这个错误
1 2 3 4 5 6 7 8 | 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 |
您应该直接使用命令和参数调用
1 | output = subprocess.Popen('you-get -o videos ' + element, shell=True, stdout=subprocess.PIPE).communicate()[0] |