Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python clear csv file

Tags:

python

csv

how can I clear a complete csv file with python. Most forum entries that cover the issue of deleting row/columns basically say, write the stuff you want to keep into a new file. I need to completely clear a file - how can I do that?

like image 591
user1266138 Avatar asked Sep 05 '12 09:09

user1266138


1 Answers

Basically you want to truncate the file, this can be any file. In this case it's a csv file so:

filename = "filewithcontents.csv"
# opening the file with w+ mode truncates the file
f = open(filename, "w+")
f.close()
like image 79
andoy Avatar answered Nov 11 '22 13:11

andoy