Python – Infinite while loop, break on user input

You can use non-blocking read from stdin: import sys import os import fcntl import time fl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL) fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, fl | os.O_NONBLOCK) while True: print(“Waiting for user input”) try: stdin = sys.stdin.read() if “\n” in stdin or “\r” in stdin: break except IOError: pass time.sleep(1)

Mixing files and loops

You get the ValueError because your code probably has for line in original: in addition to original.readline(). An easy solution which fixes the problem without making your program slower or consume more memory is changing for line in original: … to while True: line = original.readline() if not line: break …

While loop in batch

set /a countfiles-=%countfiles% This will set countfiles to 0. I think you want to decrease it by 1, so use this instead: set /a countfiles-=1 I’m not sure if the for loop will work, better try something like this: :loop cscript /nologo c:\deletefile.vbs %BACKUPDIR% set /a countfiles-=1 if %countfiles% GTR 21 goto loop