When a novice (like me) asks for reading/processing a text file in python he often gets answers like:
with open("input.txt", 'r') as f:
for line in f:
#do your stuff
Now I would like to truncate everything in the file I'm reading after a special line. After modifying the example above I use:
with open("input.txt", 'r+') as file:
for line in file:
print line.rstrip("\n\r") #for debug
if line.rstrip("\n\r")=="CC":
print "truncating!" #for debug
file.truncate();
break;
and expect it to throw away everything after the first "CC" seen. Running this code on input.txt:
AA
CC
DD
the following is printed on the console (as expected):
AA
CC
truncating!
but the file "input.txt" stays unchanged!?!?
How can that be? What I'm doing wrong?
Edit: After the operation I want the file to contain:
AA
CC
In addition to glibdud's answer, truncate() needs the size from where it deletes the content. You can get the current position in your file by the
tell()
command. As he mentioned, by using the for-loop, thenext()
prohibits commands like tell. But in the suggested while-loop, you can truncate at the current tell()-position. So the complete code would look like this:Python 3:
It looks like you're falling victim to a read-ahead buffer used internally by Python. From the documentation for the file.next() method:
The upshot is that the file's position is not where you would expect it to be when you truncate. One way around this is to use
readline
to loop over the file, rather than the iterator: