I stumbled upon the post, which tells that w+ can truncate a file, whereas r+ file mode can not.
I am having difficulties understanding what truncate means and what file.truncate() does.
I stumbled upon the post, which tells that w+ can truncate a file, whereas r+ file mode can not.
I am having difficulties understanding what truncate means and what file.truncate() does.
In crude terms, truncate is about removal of the text from your file starting from the current position until the end of the file.
Assume a file has 5 lines:
file.readline() # reads the first line
file.truncate() # removes the lines 2 through 5.
file.readline() # returns '' since lines have been deleted
file.seek(0) # moves the cursor to the start of the file
file.readline() # reads the first line again