Python子进程:命令在Python中失败,但在Shell中工作 [英] Python Subprocess: Command Fails in Python but Works in Shell
问题描述
我正在尝试通过Python的子进程运行命令,但无法正常运行.如果我键入外壳程序:
I am trying to run a command through Python's subprocess, but it won't run properly. If I type into the shell:
pack < packfile.dat
其中 pack
是我的软件,而 packfile
是输入文件,则该软件运行正常.
where pack
is my software and packfile
is the input file, then the software runs fine.
如果我在python中尝试这样做:
If I try this in python:
import subprocess as sp
import shlex
cmd = 'pack < packfile.dat'.split()
p = sp.Popen(cmd)
软件抱怨:
Pack must be run with: pack < inputfile.inp
Reading input file... (Control-C aborts)
它挂在那里.
最后一部分特定于我的软件,但事实是,运行两种命令的两种方法给出的结果不同,而情况并非如此.
This last part is specific to my software, but the fact is that the two methods of running the same command give different results, when this shouldn't be the case.
有人可以告诉我我在做什么错吗?
Can anyone tell me what I'm doing wrong?
实际上,我打算最终做到:
Actually, I intend to eventually do:
p = sp.Popen(cmd,stdout=sp.PIPE,stderr=sp.PIPE)
stdout, stderr = p.communicate()
由于我对此并不陌生,如果这不是最佳做法,请告诉我.
Since I am a little new to this, if this is not best-practice, please let me know.
谢谢.
推荐答案
<"不是命令的参数,也不应该作为参数传递.
"<" isn't a parameter to the command, and shouldn't be passed as one.
尝试:
p = sp.Popen(cmd,stdout=sp.PIPE,stderr=sp.PIPE, stdin=open('packfile.dat'))
这篇关于Python子进程:命令在Python中失败,但在Shell中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!