Open a file in write mode ('w'), and immediately close it. This truncates the file to zero length so its contents are gone but the file itself will remain. Here's the code in Python:
# Open that file in write mode to clear its contents
With open ('your_file.txt, 'w') as file :
pass # It will clear the contents of the file
method:
1.Opens a file in write mode(‘w’), which deletes all contents.
2. Close the file and empty the file instantly.
3. Now, the file will be empty, awaiting whatever new data you wish to write to it.