On Fri, Jul 15, 2011 at 21:38, Steven D'Aprano <st...@pearwood.info> wrote: > > Richard D. Moores wrote: > >> But that makes me wonder if there isn't a simpler way to do it with >> Python -- to delete the contents of a file without deleting the file? > > Opening a file for writing will flush the contents. > > open(filename, 'w') > > will do it, taking advantage of Python's garbage collector to (eventually) > close the file. The more careful way is hardly any harder: > > open(filename, 'w').close() > > and this ensures that the file isn't left open any longer than necessary.
open("C:/test/test.txt", 'w').close() Good to know. Thanks, Steven. Dick _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor