简体   繁体   中英

Checking Subprocesses in python

I'm trying to run one python program from another using subprocess . Here's the function I've got so far:

def runProcess(exe):
p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while(True):
  retcode = p.poll() #returns None while subprocess is running
  line = p.stdout.readline()
  yield line
  if(retcode is not None):
    break

then i run:

for line in runProcess('python myotherprogram.py'): print line

but I get an OS error: no such file , but it doesn't tell me what file doesn't exist. It's baffling. Any suggestions? I can use the runProcess function for normal terminal commands, such as ls .

What doesn't exist is a single executable named python myotherprogram.py . To specify arguments, you need to provide a list consisting of the command and its argument, such as with runProcess(["python", "myotherprogram.py"]) , or specify shell=True to the Popen constructor.

The relevant quote from the documentation :

args should be a sequence of program arguments or else a single string. By default, the program to execute is the first item in args if args is a sequence. If args is a string, the interpretation is platform-dependent and described below. See the shell and executable arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to pass args as a sequence.

On Unix, if args is a string, the string is interpreted as the name or path of the program to execute. However, this can only be done if not passing arguments to the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM