如何使用子流程运行此命令?
我试过:
proc = subprocess.Popen(
'''ECHO bosco|"C:\Program Files\GNU\GnuPG\gpg.exe" --batch --passphrase-fd 0 --output "c:\docume~1\usi\locals~1\temp\tmptlbxka.txt" --decrypt "test.txt.gpg"''',
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
stdout_value, stderr_value = proc.communicate()
但得到:
Traceback (most recent call last):
...
File "C:\Python24\lib\subprocess.py", line 542, in __init__
errread, errwrite)
File "C:\Python24\lib\subprocess.py", line 706, in _execute_child
startupinfo)
WindowsError: [Errno 2] The system cannot find the file specified
我注意到的事情:
首先,也最重要的是,您实际上不需要管道;您只是发送输入。你可以用subprocess.communicate来做。
其次,不要将命令指定为字符串;只要涉及到带有空格的文件名,这就很混乱。
第三,如果您真的想执行piped命令,只需调用shell。在Windows上,我相信是
cmd /c program name arguments | further stuff
。最后,单反斜杠可能很危险:
"\p"
是'\\p'
,但是'\n'
是一个新行。使用os.path.join()或os.sep,如果在python外部指定,则只使用正斜杠。你说得对,回声是问题所在。如果没有shell=True选项,则找不到ECHO命令。
失败的原因是您看到的错误:
通过:打印Ni和0
相关问题 更多 >
编程相关推荐