473,288 Members | 1,750 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,288 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 16180
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
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.