simple way to touch a file if it does not exist

bukzor workitharder at gmail.com
Thu May 22 19:45:52 EDT 2008


On May 22, 12:07 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> 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

Good to know.



More information about the Python-list mailing list