【发布时间】:2019-04-30 06:58:28
【问题描述】:
我必须使用 subprocess 包打印 bash 历史记录。
import subprocess
co = subprocess.Popen(['history'], stdout = subprocess.PIPE)
History = co.stdout.read()
print("----------History----------" + "\n" + History)
但他们提示错误
Traceback (most recent call last):
File "test.py", line 4, in <module>
co = subprocess.Popen(['history'], stdout = subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
【问题讨论】:
-
在 Popen 调用中添加
shell=Truerelevant question -
您还希望避免使用原始
Popen,如其文档中所建议的那样。或许可以进一步查看stackoverflow.com/a/51950538/874188
标签: python subprocess