【发布时间】:2020-08-01 23:19:48
【问题描述】:
我有一个名为 "abc" 的二进制可执行文件,我有一个名为 "input.txt" 的输入文件。我可以使用以下 bash 命令运行它们:
./abc < input.txt
如何在 Python 中运行此 bash 命令,我尝试了一些方法,但出现错误。
编辑: 我还需要存储命令的输出。
编辑2:
我用这种方法解决了,谢谢你的帮助。
input_path = input.txt 文件的路径。
out = subprocess.Popen(["./abc"],stdin=open(input_path),stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout,stderr = out.communicate()
print(stdout)
【问题讨论】:
-
你试过用
subprocess模块做这个吗? -
是的,我尝试了 subprocess 模块,但我不能只运行这个命令。我可以使用子进程轻松运行另一个命令,但它不适用于这个。
-
bash 符号
<表示输入重定向。看看documentation中子进程的标准输入参数
标签: python bash command executable