simple way to touch a file if it does not exist

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu May 22 03:07:13 EDT 2008


On Wed, 21 May 2008 17:56:38 -0700, bukzor wrote:

> On May 21, 5:37 pm, Nikhil <mnik... at gmail.com> wrote:
>
>> if os.path.exists('file'):
>>         open('file', 'w').close()
>>
>> Right?
> 
> You only want to blank it if it exists? If it doesn't exist you won't
> create it.
> The .close() is superlative: since you don't keep the value, it gets
> deallocated and closed by the destructor.

The language neither guarantees *when* an object will be deallocated nor
that its destructor is called *at all*.  It's cleaner to explicitly close
the file.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list