Super Newbie Question

Brian van den Broek bvande at po-box.mcgill.ca
Mon Apr 4 20:05:56 EDT 2005


joeyjwc at gmail.com said unto the world upon 2005-04-04 19:39:

<SNIP>

> I want the "engine" to read the file, write its contents to another
> temporary file (for now it just writes the contents; later it will
> format it before writing the contents) and then deletes the *contents*
> of the temporary file after printing out the result, but not the
> temporary file itself.  I want it to do this because that temporary
> file will be reused to write to for other pages as well.
> 
> In short, how might I go about deleting just the contents of a file?
> I tried several methods with my limited knowledge but had no luck.
> 
> Thank you very much for answering a lowly newbie's question.  I really
> appreciate it.
> 
> P.S.  If there's a better that you think to perform what I am saying,
> I'd like to be enlightened.  But I'd also like my question answered
> too, in case I ever need to do something similar again.

Hi,

if I understand your question, I think the answer to how to delete 
will tell you that you don't need to worry about it.

 >>> old_file = open(file_path_to_delete, 'w')
 >>> old_file.write('')
 >>> old_file.close()

That will write an empty string to the file, erasing all its contents, 
while preserving the file in your file system (as one of 0 bytes).

So, I think this shows that your worry (at least as I understood it is 
misplaced. Unless you have a free space on the disk issue at hand, you 
don't need to bother with a delete step -- just open the file in write 
mode when you want to write to it again, and that will over-write any 
extant contents.

HTH,

Brian vdB






More information about the Python-list mailing list