0

I am wondering how to run an indefinite while loop and to break it if the user (or a scripts outputting to the stdin of the present script) sends an EOF.

There are loads of specific post on SO but in all the ones I have seen, the user is asked at every iteration, for example

while True:            
    inp = input()   
    if inp != "":       
        print(2)   
    else:            
        print("I am out of here")
        break

This is different from what I am looking for. My desired behaviour is that the user does not get asked, and only if they send EOF the loop breaks.

Something like

while True:                 
    print(2)   
    # if EOF is received, break, if nothing is typed carry on looping
    break
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user37292
  • 248
  • 2
  • 12
  • 2
    Catch a `KeyboardInterrupt` exception? Usually sent with Ctrl+C. – matszwecja Apr 28 '22 at 09:22
  • You need to timeout the `input()` call otherwise the loop will wait forever for some input to be passed. Check this https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout – alec_djinn Apr 28 '22 at 09:22
  • @matszwecja, that would interrupt the whole script, I just would like to break the loop and continue with the rest of the script, the example is minimal but in the general case there might be other instructions after the while loop – user37292 Apr 28 '22 at 09:23
  • @ok will edit, the EOF interests me more I thought they were related – user37292 Apr 28 '22 at 09:24
  • just use 'try except' with KeyboardInterrupt – MercifulSory Apr 28 '22 at 09:24
  • @user37292 No, it wouldn't if you actually catched the exception. i.e `try: #loop here; except KeyboardInterrupt: #code after loop`. – matszwecja Apr 28 '22 at 09:24
  • @matszwecja, you are right of course, but I am not interested in Control+C, is EOF that matters – user37292 Apr 28 '22 at 09:26
  • @mkrieger1, I read that post but I did not see how it applies. There there is something to read and as EOF arrives, it is over. Here there is nothing to read, the loop goes on, and when EOF arrives, the loop breaks – user37292 Apr 28 '22 at 09:28
  • In order to detect EOF you have to read. If you do not care about what is read, just ignore it. – mkrieger1 Apr 28 '22 at 09:28
  • Does this answer your question? [How to read user input until EOF?](https://stackoverflow.com/questions/21235855/how-to-read-user-input-until-eof) – buhtz Apr 28 '22 at 09:30
  • @mkrieger, that is what I struggle with. There is nothing to read, I only want the loop to stop if the user sends an EOF (Control-D). Possibly I run this script, say script2.py, on the command line with another script1.py, as python3 script1.py | script2.py, and all the first script does is once in a bluemoon send an EOF. – user37292 Apr 28 '22 at 09:30
  • Then you are looking for *concurrently* waiting for EOF from stdin and doing something else meanwhile. Check out how to do concurrency in Python with either threads or asyncio. – mkrieger1 Apr 28 '22 at 09:31
  • This behaviour seems very weird to me. Need specifically EOF, but at the same time you don't care about anything before. – matszwecja Apr 28 '22 at 09:33
  • @mkrieger, thanks I will do, this sounds what I was looking for thanks. I am glad it was not a duplicate then I got concerned – user37292 Apr 28 '22 at 09:33
  • @user37292 Wouldn't it be simpler to just detect return key press? – matszwecja Apr 28 '22 at 09:35
  • See for example https://stackoverflow.com/questions/58454190/python-async-waiting-for-stdin-input-while-doing-other-stuff – mkrieger1 Apr 28 '22 at 09:35
  • @matszwecja, I appreciate it might sound weird, it is just an example to understand certain things about EOF and how input works, return key press is not relevant. – user37292 Apr 28 '22 at 09:36

0 Answers0