简体   繁体   中英

subprocess in Python hangs

I am trying to retrieve some information from a Perl script using Python and subprocess:

command = ["perl","script.perl","arg1.txt","<","arg2.txt"]
print " ".join(command)
p = subprocess.Popen(command,stdout=subprocess.PIPE,shell=True)
text = p.stdout.read()

The join-statement simply prints the command as I would enter it in the terminal for double-checking the quality of the command. That one always works... But within Python, it hangs at the subprocess.Popen() (at p= ... ).

I also tried several other methods such as call() but to no avail.

It only outputs one line of text, so I don't know how that could be the problem.

There's no need to involve the shell if you only want a simple input redirection. Open the file in Python, and pass the file handle to Popen via the stdin argument.

with open("arg2.txt") as infile:
     command = ["perl", "script.perl", "arg1.txt"]
     p = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=infile)
     text = p.stdout.read()

or

command = "perl script.perl arg1.txt < arg2.txt"
p = subprocess.Popen(command,stdout=subprocess.PIPE,shell=True)
text = p.stdout.read()

With a list and shell=True , it's not clear to me why it the call to perl blocks. When I try something like

subprocess.call("cat < .bashrc".split(), shell=True)

it blocks as if it is still trying to read from the inherited standard input. If I provide it with input using

subprocess.call("cat < .bashrc".split(), shell=True, stdin=open("/dev/null"))

the call returns immediately. In either case, it appears that cat is ignoring its further arguments.

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