2

I am trying to run a command through Python's subprocess, but it won't run properly. If I type into the shell:

pack < packfile.dat

where pack is my software and packfile is the input file, then the software runs fine.

If I try this in python:

import subprocess as sp
import shlex

cmd = 'pack < packfile.dat'.split()
p = sp.Popen(cmd)

The software complains:

Pack must be run with: pack < inputfile.inp 

Reading input file... (Control-C aborts)

and it hangs there.

This last part is specific to my software, but the fact is that the two methods of running the same command give different results, when this shouldn't be the case.

Can anyone tell me what I'm doing wrong?

Actually, I intend to eventually do:

p = sp.Popen(cmd,stdout=sp.PIPE,stderr=sp.PIPE)
stdout, stderr = p.communicate()

Since I am a little new to this, if this is not best-practice, please let me know.

Thanks in advance.

1

3 Answers 3

5

I/O redirection is a product of the shell, and by default Popen does not use one. Try this:

p = sp.Popen(cmd, shell=True)

subprocess.Popen() IO redirect

From there you'll also see that some don't prefer the shell option. In that case, you could probably accomplish it with:

with open('input.txt', 'r') as input:
    p = subprocess.Popen('./server.py', stdin=input)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your quick reply, I appreciate it. I tried it with shell=True but this unfortunately didn't work. According to the other answers, "<" can't be used in this way.
1

"<" isn't a parameter to the command, and shouldn't be passed as one.

Try:

p = sp.Popen(cmd,stdout=sp.PIPE,stderr=sp.PIPE, stdin=open('packfile.dat'))

1 Comment

Thank you! (both for providing the answer and the explanation)
1

Try this:

import subprocess as sp
p = sp.Popen(['pack'], stdin = open('packfile.dat', 'r'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.