Pergunta

I am using python 2.7 on windows 7 64 bit machine. I am calling external application within python code as

os.startfile("D:\\dist\\NewProcess.exe")

This application(used py2exe for converting python script into an exe) uses two strings, which need to pass from parent process. So, how to pass these two strings, and how to get these strings in NewProcess.py file(may be by sys.argv argument)

Foi útil?

Solução

You may try this:

import subprocess
import sys
try:
    retcode = subprocess.call("D:\\dist\\NewProcess.exe " + sys.argv[1] + " " + sys.argv[2], shell=True)
    if retcode < 0:
        print >>sys.stderr, "Child was terminated by signal", -retcode
    else:
        print >>sys.stderr, "Child returned", retcode
except OSError as e:
    print >>sys.stderr, "Execution failed:", e

In sys.argv[0] is the script name, and in sys.argv[1] ... [n] are script arguments. Example above is taken from subprocess module documentation https://docs.python.org/2/library/subprocess.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top