0

I have a python script that requires input from the user, but since i need to run this around 300 times, I was looking for a way to feed the input via bash code. My python code is in the form

import sys
inFile = eval(raw_input(sys.argv[1]))
outFile1 = eval(raw_input(sys.argv[2]))
finalfit = eval(raw_input(sys.argv[3]))

and then I go on doing my thing.

In the bash script i just wrote

python filename.py arg1 arg2 arg3

but when I try to run this, it just retuns a line with arg1, and if i hit enter, it gives me an error in the form

Traceback (most recent call last): File "narrowtry.py", line 69, in inFile = eval(raw_input(sys.argv[1]))

File "", line 0

^ SyntaxError: unexpected EOF while parsing

since the input shoud be a string, i tried using

3 Answers3

2

Never use eval to parse user input! It could do dangerous things.

With that said, take a look at what your code is doing, in interpreter mode:

>>> eval(raw_input('arg1'))
arg1           # press enter here
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing
>>> 

sys.argv[1] already contains arg1. You are then passing this as a parameter to the raw_input function, which will then expect user input. Since you pass nothing, eval receives only a newline character, and throws an error.

You fix this by doing:

inFile = sys.argv[1]
cs95
  • 379,657
  • 97
  • 704
  • 746
1

Don't use eval! Especially for user input. It's dangerous.

If those args are your input, you don't even need raw_input; just set inFile = sys.argv[1] etc.

But you should think it through what the original was trying to do with you args (and why eval was completely unnecessary, even wrong). You'll run into this again in the future.

0

You want to pass the arguments to stdin of the Python script, not pass them as command-line arguments as you have now. In Bash you can do this:

python filename.py <<< arg1 arg2 arg3
John Zwinck
  • 239,568
  • 38
  • 324
  • 436