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

Discussion in 'Python' started by noydb, Jan 30, 2013.

  1. noydb

    noydb Guest

    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
    noydb, Jan 30, 2013
    #1
    1. Advertising

  2. noydb

    noydb Guest

    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???
    noydb, Jan 30, 2013
    #2
    1. Advertising

  3. noydb

    Chris Rebert Guest

    On Wed, Jan 30, 2013 at 9:15 AM, noydb <> wrote:
    > 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
    Chris Rebert, Jan 30, 2013
    #3
  4. noydb

    MRAB Guest

    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)
    MRAB, Jan 30, 2013
    #4
  5. noydb

    noydb Guest

    oh DUH! that was it, just missing the -F. Thank-you!!
    noydb, Jan 30, 2013
    #5
  6. noydb

    Terry Reedy Guest

    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
    Terry Reedy, Jan 30, 2013
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Varn
    Replies:
    2
    Views:
    614
    Ken Varn
    Jun 22, 2005
  2. Replies:
    3
    Views:
    483
    David Eppstein
    Sep 17, 2003
  3. Pierre Fortin

    args v. *args passed to: os.path.join()

    Pierre Fortin, Sep 18, 2004, in forum: Python
    Replies:
    2
    Views:
    672
    Pierre Fortin
    Sep 18, 2004
  4. er
    Replies:
    2
    Views:
    488
  5. Andrew Tomazos
    Replies:
    5
    Views:
    562
Loading...

Share This Page