Since raw_input
is blocking your code, you might wanna separate the process in two threads: the main
one and one that you create in your code. Since threads run concurrently and in an unpredictable order (kinda), you're not gonna be able to control exactly on what line the interruption is gonna reach your main while
loop). Threads are a very tricky part to get right and it requires a lot of reading, testing and checking why things happen the way they happen...
Also, since you don't mind consuming your lines, you can do what's called a destructive read: Load the contents of the file into a lines
variable, and keep getting the last one with pop()
until you run out of lines to consume (or the flag has been activated). Check what a pop() method does in a list. Be aware that pop()
always returns the last item of a list. If you want the items printed in the original order, you have to use shift or pop
from a previously reversed list.
import threading
interrupt=None
def flag_activator():
global interrupt
interrupt = raw_input("(!!) Type yes when you wanna stop\n\n")
print "Oh gosh! The user input %s" % interrupt
th = threading.Thread(target=flag_activator)
th.start()
fr = open('q0.txt', 'r')
lines = fr.readlines()
fr.close()
while lines and interrupt != 'yes':
print "I read this line: %s" % lines.pop()
if len(lines) > 0:
print "Crap! There are still lines"
fw = open('q0.txt', 'w')
fw.writelines(lines)
fw.close()
Now, that code is gonna block your terminal until you type yes
on the terminal.
PS: Don't forget to close your opened files (if you don't want to call close()
explicitly, see the with
statement here and here)
EDIT (as per OP's comments to my misunderstanding):
If what you want is to ensure that the file will not contain the already processed line if your script suddenly stops, an inefficient (but straightforward) way to accomplish that is:
- Open the file for read and write (you're gonna need a different file descriptor for each operation)
- Load all the file's lines into a variable
- Process the first line
- Remove that line from the list variable
- Write the remaining list to the file
- Repeat until no more lines are loaded.
All this opening/closing of files is really, really inefficient, though, but here it goes:
done = False
while done == False:
with open("q0.txt", 'r') as fr, open("q0.txt", 'w') as fw:
lines = fr.readlines()
if len(lines) > 0:
print lines[0] # This would be your processing
del lines[0]
fw.writelines(lines)
else:
done = True