By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

simple way to touch a file if it does not exist

P: n/a
what are the simple ways?
I could think of os.open(), os.exec(touch file)

are there any simpler methods?
Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 22 Mag, 01:15, Nikhil <mnik...@gmail.comwrote:
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/
Jun 27 '08 #2

P: n/a
On May 21, 5:10 pm, "Giampaolo Rodola'" <gne...@gmail.comwrote:
On 22 Mag, 01:15, Nikhil <mnik...@gmail.comwrote:
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.
Jun 27 '08 #3

P: n/a
bukzor wrote:
On May 21, 5:10 pm, "Giampaolo Rodola'" <gne...@gmail.comwrote:
>On 22 Mag, 01:15, Nikhil <mnik...@gmail.comwrote:
>>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?
Jun 27 '08 #4

P: n/a
On May 21, 5:37 pm, Nikhil <mnik...@gmail.comwrote:
bukzor wrote:
On May 21, 5:10 pm, "Giampaolo Rodola'" <gne...@gmail.comwrote:
On 22 Mag, 01:15, Nikhil <mnik...@gmail.comwrote:
>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.
Jun 27 '08 #5

P: n/a
On Wed, 21 May 2008 17:56:38 -0700, bukzor wrote:
On May 21, 5:37 pm, Nikhil <mnik...@gmail.comwrote:
>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
Jun 27 '08 #6

P: n/a
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.comwrote:
>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/
Jun 27 '08 #7

P: n/a
On 22 Mag, 11:54, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
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.comwrote:
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
Jun 27 '08 #8

P: n/a
On May 22, 12:07 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Wed, 21 May 2008 17:56:38 -0700, bukzor wrote:
On May 21, 5:37 pm, Nikhil <mnik...@gmail.comwrote:
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.
Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.