how to use subprocess to execute an exe with args and an output

N

noydb

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
 
N

noydb

To add on, I need to eventually write a script that takes input, feeds it into this exe, and takes the output file to perform more 'tools'/manipulations on it. Is call or Popen called for, why? Or maybe some other module???
 
C

Chris Rebert

I am looking for some guidance on using subprocess to execute an EXE witharguments and an output. The below code works in that it returns a 0 exitcode, 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 my cursory reading of GPSBabel's documentation is right, you're
missing a "-F" before the output filepath. Try:

subprocess.call([
r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
"-i", "gdb",
"-f", r"C:\Temp\GDBdata\testgps28.gdb",
"-o", "gpx",
"-F", r"C:\Temp\gpx\test28output.gpx",
])

Regards,
Chris
 
M

MRAB

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)
 
T

Terry Reedy

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top