Calling python script in dos and passing arguments

Discussion in 'Python' started by PEnergy, Apr 16, 2013.

  1. PEnergy

    PEnergy Guest

    Greetings,

    I am trying to write a python script that, when called from the DOS prompt, will call another python script and pass it input variables. My current code will open the other python script but doesn't seem to pass it any values:

    import os,sys,subprocess
    subprocess.Popen(['python.exe','C:\NDEX\GRE2\uip\uip_20.py','t3c*'])

    Am I missing something or is this type of call not possible through DOS?

    Thanks,
    PEnergy
    PEnergy, Apr 16, 2013
    #1
    1. Advertising

  2. PEnergy

    Chris Rebert Guest

    On Tue, Apr 16, 2013 at 7:14 AM, PEnergy <> wrote:
    > Greetings,
    >
    > I am trying to write a python script that, when called from the DOS prompt, will call another python script and pass it input variables. My current code will open the other python script but doesn't seem to pass it any values:
    >
    > import os,sys,subprocess
    > subprocess.Popen(['python.exe','C:\NDEX\GRE2\uip\uip_20.py','t3c*'])
    >
    > Am I missing something or is this type of call not possible through DOS?


    1. Backslash is an escape character in Python strings (e.g. "\n" =
    newline). You should therefore double-up on your backslashes. (Your
    exact string just so happens to work due to a misfeature regarding how
    invalid backslash escapes are handled.)
    2. Glob/wildcard ("*") expansion is done by the shell, but
    subprocess.Popen does not use the shell by default (for good reason!).
    Use the `glob` library to do the expansion yourself, in Python:
    http://docs.python.org/2/library/glob.html

    Cheers,
    Chris
    Chris Rebert, Apr 16, 2013
    #2
    1. Advertising

  3. On 04/16/2013 08:14 AM, PEnergy wrote:
    > Greetings,
    >
    > I am trying to write a python script that, when called from the DOS
    > prompt, will call another python script and pass it input variables.
    > My current code will open the other python script but doesn't seem to
    > pass it any values:
    >
    > import os,sys,subprocess
    > subprocess.Popen(['python.exe','C:\NDEX\GRE2\uip\uip_20.py','t3c*'])


    I find it easier just to write my python programs such that they can be
    imported into other python scripts. Usually I use this form:

    def func(*whatever):
    pass

    if __name__ == "__main__":
    # parse command-line arguments
    # call functions in this module with those args
    func(1,2,3,etc)

    This way a script can be run standalone, or I can import it and access
    its attributes direction. I recommend you consider this approach.
    Michael Torrie, Apr 16, 2013
    #3
    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.

Share This Page