0

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?

speedyrazor
  • 3,127
  • 7
  • 33
  • 51
  • err... your example code is using `shell=False`? – roippi May 23 '14 at 22:41
  • Yes, shell=False, can't get it working. – speedyrazor May 24 '14 at 04:52
  • Please post the output of `print repr(shlex.split(command))`, with the password crossed-out. – user4815162342 May 24 '14 at 07:11
  • I see the problem, but still unsure on how to fix it. Here's the output: ['C:progra~1itmsiTMSTransporter', '-m', 'verify', '-f', 'C:Temptest1.itmsp', '-u', 'username', '-p', 'password', '-o', 'C:Temptest1.itmspLogFile.txt', '-s', 'provider', '-v', 'eXtreme'] – speedyrazor May 24 '14 at 07:15
  • I also just tried using: process = Popen(shlex.split(command, posix = False), shell=False, stdin=PIPE) to get a print of: ['C:\\progra~1\\itms\\iTMSTransporter', '-m', 'verify', '-f', 'C:\\Temp\\test1.itmsp', '-u', 'username', '-p', 'password', '-o', 'C:\\Temp\\test1.itmsp\\LogFile.txt', '-s', 'provider', '-v', 'eXtreme'] but still get the same error message. still unsure how to fix. – speedyrazor May 24 '14 at 07:43
  • is `stdin=PIPE` intended? – Bort May 25 '14 at 19:31
  • Does it make any difference? – speedyrazor May 25 '14 at 19:53

2 Answers2

0

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.

speedyrazor
  • 3,127
  • 7
  • 33
  • 51
0

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.

Community
  • 1
  • 1
kxr
  • 4,841
  • 1
  • 49
  • 32