Truncating A Text File Does Not Change The File
Solution 1:
It looks like you're falling victim to a read-ahead buffer used internally by Python. From the documentation for the file.next() method:
A file object is its own iterator, for example
iter(f)
returns f (unless f is closed). When a file is used as an iterator, typically in afor
loop (for example,for line in f: print line.strip()
), thenext()
method is called repeatedly. This method returns the next input line, or raisesStopIteration
when EOF is hit when the file is open for reading (behavior is undefined when the file is open for writing). In order to make afor
loop the most efficient way of looping over the lines of a file (a very common operation), thenext()
method uses a hidden read-ahead buffer. As a consequence of using a read-ahead buffer, combiningnext()
with other file methods (likereadline()
) does not work right. However, usingseek()
to reposition the file to an absolute position will flush the read-ahead buffer.
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:
line = file.readline()
while line:
...
line = file.readline()
Solution 2:
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, the next()
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:
withopen("test.txt", 'r+') as file:
line = file.readline()
while line:
print(line.strip())
if line.strip() == "CC":
print("truncating")
file.truncate(file.tell())
break
line = file.readline()
Post a Comment for "Truncating A Text File Does Not Change The File"