Forums

subprocess.Popen reading in stdin as python

I'm trying to send .txt input to a python script using subprocess.Popen. My problem is python_anywhere keeps trying to run the input stdin as python.

I tried the exact same code on replit.com and it worked. What's going on?

Here is my code:

myinput = open('work/input_file.txt')   # input file -NOT python
myoutput = open('work/results.txt')     # output file
p = subprocess.Popen(['python', 'work/testFile.py'], stdin=myinput, stdout=myoutput, text=True)

Stored in testFile.py is a simple script:

a = input().split()
print(a)

Stored in input_file.txt is a simple text:

a b c d e f g

I get the following error:

Traceback (most recent call last):
File "work/testFile.py", line 1, in <module>
a = input().split()
File "<string>", line 1
a b c d e f g
  ^
SyntaxError: invalid syntax

Even stranger is that if I change the code:

testFile.py would be

a =input()
print(a**3)

input_file.txt is:

3

I should get an error but I don't. Instead pythonAnywhere processes the input as an integer without complaint? Why is this happening?

I just realized something. the command was "python3.8" not "python"

Anyway, I read that you limit subprocesses in some forum. How does that work?

Yes, we limit the number of processes that you can run at the same time on the same machine, but it's a pretty high number so you shouldn't have any trouble with the code from your first post.