best way to check if a file exists?

J

John Salerno

What is the best way to check if a file already exists in the current
directory? I saw os.path.isfile(), but I'm not sure if that does more
than what I need.

I just want to check if a file of a certain name exists before the user
creates a new file of that name.

Thanks.
 
U

utabintarbo

John said:
What is the best way to check if a file already exists in the current
directory? I saw os.path.isfile(), but I'm not sure if that does more
than what I need.

I just want to check if a file of a certain name exists before the user
creates a new file of that name.

Thanks.

os.path.exists()?
 
G

Gabriel Genellina

What is the best way to check if a file already exists in the current
directory? I saw os.path.isfile(), but I'm not sure if that does more
than what I need.

os.access(full_filename, os.F_OK)
http://docs.python.org/lib/os-file-dir.html
I just want to check if a file of a certain name exists before the user
creates a new file of that name.

Remember that things may change between you check the name and you
actually create the file.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
G

georgeryoung

What is the best way to check if a file already exists in the current
directory? I saw os.path.isfile(), but I'm not sure if that does more
than what I need.

I just want to check if a file of a certain name exists before the user
creates a new file of that name.

You could be more "pythonic", and simply try to create the file,
catching the exception if if fails. This works on linux:

try:
newfd = os.open('foobar', os.O_EXCL | os.O_CREAT)
new_file = os.fdopen(newdf)
except OSError, x:
if x[1] == 'File exists':
handle_file_exists()

[but beware unreliable on an NFS file system, from "man open":
O_EXCL is broken on NFS file
systems, programs which rely on it for performing locking tasks
will contain a race condition. The solution for performing atomic
file locking using a lockfile is to create a unique file on the
same fs (e.g., incorporating hostname and pid), use link(2) to make
a link to the lockfile. If link() returns 0, the lock is
successful.­
Otherwise, use stat(2) on the unique file to check if its
link count has increased to 2, in which case the lock is also
suc­cessful.]
 
B

Ben Finney

I see that 'os.path.exists' has already been pointed out in a later
post.
You could be more "pythonic", and simply try to create the file,
catching the exception if if fails. This works on linux:

try:
newfd = os.open('foobar', os.O_EXCL | os.O_CREAT)
new_file = os.fdopen(newdf)
except OSError, x:
if x[1] == 'File exists':
handle_file_exists()

Are you sure that both of 'os.open' *and* 'os.fdopen' will only ever
raise OSError if the file already exists, and not for any other OS
errors?
 
J

jim-on-linux

What is the best way to check if a file already
exists in the current directory? I saw
os.path.isfile(), but I'm not sure if that does
more than what I need.

I just want to check if a file of a certain
name exists before the user creates a new file
of that name.

Thanks.


How about something like one of these;

if os.path.isfile(vfileName) not True :
male file
or
if os.path.isfile (os.path.join(os.getcwd(),
vFileName) )==True :
do something


jim-on-linux

http://www.inqvista.com
 
W

wittempj

John said:
What is the best way to check if a file already exists in the current
directory? I saw os.path.isfile(), but I'm not sure if that does more
than what I need.

I just want to check if a file of a certain name exists before the user
creates a new file of that name.

Thanks.

You could try to read the file, if that fails it doesn't exist:

try:
f = open('xxx')
except IOError:
f = open('xxx', 'w')
print "file doesn't exist"
print f
 
B

Ben Finney

You could try to read the file, if that fails it doesn't exist:

try:
f = open('xxx')
except IOError:
f = open('xxx', 'w')
print "file doesn't exist"
print f

Except that there are other conditions than "File doesn't exist" that
can cause an 'open' to fail.
 
W

wittempj

Ben said:
Except that there are other conditions than "File doesn't exist" that
can cause an 'open' to fail.
Ok, true. You can test explicit on non existence as follows, and then
decide to open the file

import errno
try:
f = open('xxx')
except IOError, e:
if e.errno == errno.ENOENT:
f = open('xxx', 'w')
print f
 
S

sjdevnull

Ben said:
Or you can simply use 'os.path.exists', as has been suggested several
times in this thread.

The O_CREAT|O_EXCL method pointed out elsethread is preferrable,
checking with "exists" and then creating if it returns false is racey.
 
F

Fredrik Lundh

os.path.exists() checks if a path exists, so a directory as argument
also returns True

the original requirement was "to check if a file of a certain name
exists before the user creates a new file of that name".

if you want to check for files only, use the os.path.isfile predicate.

</F>
 
S

Steven D'Aprano

Or you can simply use 'os.path.exists', as has been suggested several
times in this thread.

But there can be a race condition between os.path.exists returning True
and you trying to open the file, if some other process deletes or renames
the file in the meantime.
 
F

Fredrik Lundh

Steven said:
But there can be a race condition between os.path.exists returning True
and you trying to open the file, if some other process deletes or renames
the file in the meantime.

if you'd used a threaded newsreader, you would have seen the code that
"as follows" was referring to, and you would probably also have noticed
that people have been pointing out the potential race problems several
times already.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top