Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

simple way to touch a file if it does not exist

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


mnikhil at gmail

May 21, 2008, 4:15 PM

Post #1 of 9 (37827 views)
Permalink
simple way to touch a file if it does not exist

what are the simple ways?
I could think of os.open(), os.exec(touch file)

are there any simpler methods?
--
http://mail.python.org/mailman/listinfo/python-list


gnewsg at gmail

May 21, 2008, 5:10 PM

Post #2 of 9 (37735 views)
Permalink
Re: simple way to touch a file if it does not exist [In reply to]

On 22 Mag, 01:15, Nikhil <mnik...@gmail.com> wrote:
> what are the simple ways?
> I could think of os.open(), os.exec(touch file)
>
> are there any simpler methods?

Just use os.path.exists to check for file existence and open() as
replacement for touch.

>>> import os
>>> if not os.path.exists('file'):
... open('file', 'w').close()
...
>>>


--- Giampaolo
http://code.google.com/p/pyftpdlib/
--
http://mail.python.org/mailman/listinfo/python-list


workitharder at gmail

May 21, 2008, 5:21 PM

Post #3 of 9 (37735 views)
Permalink
Re: simple way to touch a file if it does not exist [In reply to]

On May 21, 5:10 pm, "Giampaolo Rodola'" <gne...@gmail.com> wrote:
> On 22 Mag, 01:15, Nikhil <mnik...@gmail.com> wrote:
>
> > what are the simple ways?
> > I could think of os.open(), os.exec(touch file)
>
> > are there any simpler methods?
>
> Just use os.path.exists to check for file existence and open() as
> replacement for touch.
>
> >>> import os
> >>> if not os.path.exists('file'):
>
> ... open('file', 'w').close()
> ...
>
>
>
> --- Giampaolohttp://code.google.com/p/pyftpdlib/

As simple as it gets is a single builtin function call:

open("somefile.txt", "a")

Leave out the ,"a" if you don't mind blanking a pre-existing file.
--
http://mail.python.org/mailman/listinfo/python-list


mnikhil at gmail

May 21, 2008, 5:37 PM

Post #4 of 9 (37733 views)
Permalink
Re: simple way to touch a file if it does not exist [In reply to]

bukzor wrote:
> On May 21, 5:10 pm, "Giampaolo Rodola'" <gne...@gmail.com> wrote:
>> On 22 Mag, 01:15, Nikhil <mnik...@gmail.com> wrote:
>>
>>> what are the simple ways?
>>> I could think of os.open(), os.exec(touch file)
>>> are there any simpler methods?
>> Just use os.path.exists to check for file existence and open() as
>> replacement for touch.
>>
>>>>> import os
>>>>> if not os.path.exists('file'):
>> ... open('file', 'w').close()
>> ...
>>
>>
>>
>> --- Giampaolohttp://code.google.com/p/pyftpdlib/
>
> As simple as it gets is a single builtin function call:
>
> open("somefile.txt", "a")
>
> Leave out the ,"a" if you don't mind blanking a pre-existing file.
Thanks :-)

That reminds me to check if I could quickly nullify a file if it exists

if os.path.exists('file'):
open('file', 'w').close()

Right?
--
http://mail.python.org/mailman/listinfo/python-list


workitharder at gmail

May 21, 2008, 5:56 PM

Post #5 of 9 (37731 views)
Permalink
Re: simple way to touch a file if it does not exist [In reply to]

On May 21, 5:37 pm, Nikhil <mnik...@gmail.com> wrote:
> bukzor wrote:
> > On May 21, 5:10 pm, "Giampaolo Rodola'" <gne...@gmail.com> wrote:
> >> On 22 Mag, 01:15, Nikhil <mnik...@gmail.com> wrote:
>
> >>> what are the simple ways?
> >>> I could think of os.open(), os.exec(touch file)
> >>> are there any simpler methods?
> >> Just use os.path.exists to check for file existence and open() as
> >> replacement for touch.
>
> >>>>> import os
> >>>>> if not os.path.exists('file'):
> >> ... open('file', 'w').close()
> >> ...
>
> >> --- Giampaolohttp://code.google.com/p/pyftpdlib/
>
> > As simple as it gets is a single builtin function call:
>
> > open("somefile.txt", "a")
>
> > Leave out the ,"a" if you don't mind blanking a pre-existing file.
>
> Thanks :-)
>
> That reminds me to check if I could quickly nullify a file if it exists
>
> 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.
--
http://mail.python.org/mailman/listinfo/python-list


bj_666 at gmx

May 22, 2008, 12:07 AM

Post #6 of 9 (37725 views)
Permalink
Re: simple way to touch a file if it does not exist [In reply to]

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

> On May 21, 5:37 pm, Nikhil <mnik...@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
--
http://mail.python.org/mailman/listinfo/python-list


straton at lampsacos

May 22, 2008, 2:54 AM

Post #7 of 9 (37717 views)
Permalink
Re: simple way to touch a file if it does not exist [In reply to]

After os.path.exists, you need to check it _is_ a
file, and not a directory.

Giampaolo Rodola' wrote:
> On 22 Mag, 01:15, Nikhil <mnik...@gmail.com> wrote:
>> what are the simple ways?
>> I could think of os.open(), os.exec(touch file)
>>
>> are there any simpler methods?
>
> Just use os.path.exists to check for file existence and open() as
> replacement for touch.
>
>>>> import os
>>>> if not os.path.exists('file'):
> ... open('file', 'w').close()
> ...
>
>
> --- Giampaolo
> http://code.google.com/p/pyftpdlib/
--
http://mail.python.org/mailman/listinfo/python-list


gnewsg at gmail

May 22, 2008, 5:58 AM

Post #8 of 9 (37717 views)
Permalink
Re: simple way to touch a file if it does not exist [In reply to]

On 22 Mag, 11:54, Ken Starks <stra...@lampsacos.demon.co.uk> wrote:
> After os.path.exists, you need to check it _is_ a
> file, and not a directory.
>
>
>
> Giampaolo Rodola' wrote:
> > On 22 Mag, 01:15, Nikhil <mnik...@gmail.com> wrote:
> >> what are the simple ways?
> >> I could think of os.open(), os.exec(touch file)
>
> >> are there any simpler methods?
>
> > Just use os.path.exists to check for file existence and open() as
> > replacement for touch.
>
> >>>> import os
> >>>> if not os.path.exists('file'):
> > ...     open('file', 'w').close()
> > ...
>
> > --- Giampaolo
> >http://code.google.com/p/pyftpdlib/- Nascondi testo tra virgolette -
>
> - Mostra testo tra virgolette -

Yeah, I forgot. :\


--- Giampaolo
http://code.google.com/p/pyftpdlib
--
http://mail.python.org/mailman/listinfo/python-list


workitharder at gmail

May 22, 2008, 4:45 PM

Post #9 of 9 (37696 views)
Permalink
Re: simple way to touch a file if it does not exist [In reply to]

On May 22, 12:07 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
> On Wed, 21 May 2008 17:56:38 -0700, bukzor wrote:
> > On May 21, 5:37 pm, Nikhil <mnik...@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.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.