how to use subprocess to execute an exe with args and an output
Terry Reedy
tjreedy at udel.edu
Wed Jan 30 14:58:19 EST 2013
On 1/30/2013 1:11 PM, MRAB wrote:
> On 2013-01-30 17:15, noydb wrote:
>> I am looking for some guidance on using subprocess to execute an EXE
>> with arguments and an output. The below code works in that it returns
>> a 0 exit code, but no output file is created. I have tried a few
>> different versions of this code (used Popen instead, some
>> stderr/stdout), but no luck. Can anyone offer an explanation or
>> suggestion? (GPSBabel is freeware)
>> Python 2.7 on windows7 64-bit
>>
>> import subprocess
>> subprocess.call([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
>> "-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
>> "-o", "gpx", r"C:\Temp\gpx\test28output.gpx"])
>>
>> If I use this below, I get a returncode of 1, exit code of 0.
>> import subprocess
>> x = subprocess.Popen([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
>> "-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
>> "-o", "gpx", r"C:\Temp\gpx\test28output.gpx",
>> "shell=True", "stdout=subprocess.PIPE",
>> "stderr=subprocess.PIPE"])
>>
>> x.wait()
>> print x.returncode
>>
>> Thanks in advance for any help
>>
> The second example is incorrect. The parts starting from "shell" are
> supposed to be further arguments for Popen itself, not something passed
> to "gpsbabel.exe":
>
> x = subprocess.Popen([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
> "-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
> "-o", "gpx", r"C:\Temp\gpx\test28output.gpx"],
> shell=True, stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
>
and it is apparently best to not use shell=True unless actually needed
for shell processing, which I do not think is the case here.
--
Terry Jan Reedy
More information about the Python-list
mailing list