-1

Does anyone know how to clear a file's contents on python? Thank you.

crvcio
  • 321
  • 2
  • 5
  • 11

2 Answers2

1

Opening a file creates it and (unless append ('a') is set) overwrites it with emptyness, such as this:

open(filename, 'w').close()

See: How to empty a file using Python

Robindpw
  • 58
  • 7
0

You can use truncate function on the file object:

file_object = open('test.txt','r+')
file_object.truncate()
file_object.close()
Tomato Master
  • 496
  • 3
  • 10