Skip to content Skip to sidebar Skip to footer

Subprocess.call Doesn't Work As Expected

I have the following batch file (test.bat) my.py < commands.txt my.py does the following: import sys print sys.stdin.readlines() Everything works fine if I launch this batch

Solution 1:

It should be:

rc = subprocess.call(["cmd", "/c", "/path/to/test.bat"])

Or using the shell:

rc = subprocess.call("cmd /c /path/to/test.bat", shell=True)

Solution 2:

Does this work:

from subprocess import *

rc = Popen("test.bat", shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
print rc.stdout.readline()
print rc.stderr.readline()

rc.stdout.close()
rc.stdin.close()
rc.stderr.close()

I'm not sure why you refer to:

my.py < commands.txt

Does the input has anything to do with the bat-file? If so do you call:

python my.py

which opens the batfile via subprocess that does:

batfile < commands.txt

or why is that relevant?

Post a Comment for "Subprocess.call Doesn't Work As Expected"