1 Replies - 107 Views - Last Post: 18 August 2014 - 01:46 AM Rate Topic: -----

#1 jp612  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 16-August 11

Taking file as stdin. EOF when getting user input

Posted 18 August 2014 - 01:00 AM

Hi,

I have a script which sometimes will take input as files, like so:
python prog.py < file1



I then have a loop which takes the input:
mode = os.fstat(0).st_mode

While True:
if stat.S_ISREG(mode):        
        r = sys.stdin.readlines()

        inp = input()



In the above code I check whether the file is redirected. If it is I take all the input and send it to a list.

This works fine, it takes the input from file1 and puts it in the list. However after that I would like to accept user input again. But when I try call input() it just raises and EOF exception because I took everything out of the file.

How can I tell stdin to start accepting user input again?

This post has been edited by jp612: 18 August 2014 - 01:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Taking file as stdin. EOF when getting user input

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: Taking file as stdin. EOF when getting user input

Posted 18 August 2014 - 01:46 AM

If you redirect stdin, you can't take input from the user (without operating system-specific code at least). If you want to get input from the user and from a file, why not just take the file name as an argument?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1