I have text file which I want to erase in Python. How do I do that?
Or alternatively, if you have already an opened file:
f = open('file.txt', 'r+') f.truncate(0) # need '0' when using r+
In C++, you could use something similar.
Not a complete answer more of an extension to ondra's answer
truncate() ( my preferred method ) make sure your cursor is at the required position.
When a new file is opened for reading -
open('FILE_NAME','r') it's cursor is at 0 by default.
But if you have parsed the file within your code, make sure to point at the beginning of the file again i.e
truncate() truncates the contents of a file starting from the current cusror position.