[Tutor] how to clear contents of a file

Christian Witts cwitts at compuscan.co.za
Wed Feb 10 07:43:08 CET 2010


sudhir prasad wrote:
> hi,
> how to clear contents of a file with out actually deleting it,
> basically wat im trying to do is copy a source file into  a common 
> file ,run the common file,after that  i need to copy another source 
> file  into the common file,i ant to clear the  contents of the common 
> file before copying contents into it.how does f.truncate() helps here
>
>
> thanks,
> sudheer
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>   
You can just open the file again in write mode which will truncate it 
for you.
So, f = open(filename, 'w') will clear the contents of your filename 
(don't forget to close the file when you're done if you do this though).

-- 
Kind Regards,
Christian Witts




More information about the Tutor mailing list