Deleting or Empty a File

Daniel Nogradi nogradi at gmail.com
Tue Apr 17 07:49:26 EDT 2007


> I'm looking to clear those log files we were discussing earlier chaps,
>
>
> What the best method to do this? Delete the file completely? Or just empty
> its content?


If you just want to delete the content of a file but keep it with 0
length you can do:

f = open( 'myfile', 'w' )    # or open( 'myfile', 'wb' )
f.close( )

Since you mentioned log files, you might want to look at the logging module:

http://docs.python.org/lib/module-logging.html

HTH,
Daniel



More information about the Python-list mailing list