0

I want to pass some data to a python script using echo and after that promote the user to input options. I am running through an EOFError which I think is happening since I read all data in sys.stdin. How do I fix this issue? Thanks!

code.py:

x = ''
for line in sys.stdin:
  x += line
y = raw_input()

usage:

echo -e -n '1324' | ./code.py

error at raw_input():

EOFError: EOF when reading a line

2 Answers 2

1

Use:

{ echo -e -n '1324'; cat; } | ./code.py

First echo will write the literal string to the pipe, then cat will read from standard input and copy that to the pipe. The python script will see this all as its standard input.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you type something? You need to type stuff and then type Ctl-d to signal EOF.
I think I now understand what you meant. Thanks
0

You just cannot send data through stdin (that's redirecting) and then get back the interactive mode.

When you perform a | b, b cannot read from standard input anymore. If it wants to do that, it will stop as soon as a finishes and breaks the pipe.

But when a finishes, it does not mean than you get hold of stdin again.

Maybe you could change the way you want to do things, example:

echo -n -e '1324' | ./code.py

becomes

./code.py '1234' '5678'

and use sys.argv[] to get the value of 1234, 5678...

import sys

x = ''
for line in sys.argv[1:]:
  x += line+"\n"

y = raw_input()

if you have a lot of lines to output, pass an argument which is a file and what you'll read

import sys

x = ''
for line in open(sys.argv[1],"r"):
  x += line

y = raw_input()

Comments

Your Answer

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