I am trying to get a Popen command running on Windows with a shell=False and for the life of me I can't get it working. iTMSTransporter is a command line application. This is my current code:

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)

Which gives me the error:

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

I'm sure progra~1 is not the issue as this works with shell=True, and have also tried Program Files, same result. Any ideas to get this working on Windows platform, Python 2.7?

2

There are 2 best solutions below

0
On BEST ANSWER

after much research I have discovered how to do this, for reference, this is how I did it:

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)

Adding the .cmd to the input file and removing the shlex module did the trick, plus adding in the code to stop the command window opening.

0
On

shlex.split is not suitable for Windows command-lines (so far), even with posix=0, and is not necessary. For futher details see python, windows : parsing command lines with shlex .

The shlex class makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix shell.