473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?
Jun 27 '08 #1
8 16187
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: pekka niiranen | last post by:
Does anybody know Python recipe for changing the date of the directory or files in W2K to current date and time? In UNIX shell command "touch" does it. -pekka-
2
by: Robert May | last post by:
I have an application that processes text strings using information in a file. I want to deploy this app using no-touch deployment. The config files and everything else are deployed just fine,...
3
by: Marek | last post by:
Hi there, my WinForm application has to be installed in "no-touch-deployment" mode, so users can run it from the web. I need to read application's .config file but it does not work in ...
0
by: John Ross | last post by:
Enterprise Library - No Touch Deployment: I have used this website and I cannot get to it. http://blog.hishambaz.com/archive/2005/01/29/194.aspx Invalid section name. The section...
4
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
1
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
14
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
2
by: Chuck Anderson | last post by:
I've got a script that "syncs" files from my working hard drive (Windows XP) to a backup drive based on filesize and filemtime. It's never had problems at the DST switch before, but since...
0
by: myself337 | last post by:
in VB you can do this: Imports System Imports System.IO Module Module1 Sub Main() Dim path As String = "c:\temp\filetocopy.txt" ' put the file you
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.