Popen(shlex.split(命令),shell = False,stdin = PIPE)错误WindowsError:[错误2]系统找不到指定的文件

时间:2014-05-23 22:04:40

标签: python windows popen shlex

我试图让一个Popen命令在Windows上运行,shell = False,在我的生命中,我无法让它运行起来。 iTMSTransporter是一个命令行应用程序。这是我目前的代码:

import os
import shlex
import subprocess
from subprocess import Popen, PIPE, STDOUT

link = "C:/progra~1/itms/iTMSTransporter"
link1 = os.path.normpath(link)
link2 = "C:/Temp/test1.itmsp"
link3 = os.path.normpath(link2)
link4 = os.path.join(link3, "LogFile.txt")

command = link1 + " -m verify -f " + link3 +" -u username -p password -o " + link4 + " -s provider -v eXtreme"
process = Popen(shlex.split(command), shell=False, stdin=PIPE)

这给了我错误:

Traceback (most recent call last):
  File "C:\Temp\temp.py", line 13, in <module>
    process = Popen(shlex.split(command), shell=False, stdin=PIPE)
  File "C:\Python27\lib\subprocess.py", line 709, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

我确定progra~1不是问题因为这适用于shell = True,并且还尝试过Program Files,结果相同。 有什么想法让这个在Windows平台上运行,Python 2.7?

2 个答案:

答案 0 :(得分:0)

经过大量研究后我发现了如何做到这一点,作为参考,我就是这样做的:

import os
import subprocess
from subprocess import Popen, PIPE, STDOUT

link = "C:/progra~1/itms/iTMSTransporter.cmd"
link1 = os.path.normpath(link)
link2 = "C:/Temp/test1.itmsp"
link3 = os.path.normpath(link2)
link4 = os.path.join(link3, "LogFile.txt")

command = link1 + " -m verify -f " + link3 +" -u username -p password -o " + link4 + " -s provider -v eXtreme"
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
process = Popen(command, startupinfo=startupinfo, shell=False)

将.cmd添加到输入文件并删除shlex模块就可以了,并添加代码以停止打开命令窗口。

答案 1 :(得分:0)

<a name="friday" id="friday"><strong>FRIDAY Index <br> </strong></a> 不适用于Windows命令行(到目前为止),即使使用shlex.split,也不是必需的。有关详细信息,请参阅python, windows : parsing command lines with shlex

  

shlex类可以轻松编写简单的词法分析器   语法类似于Unix shell