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

best way to check if a file exists?

P: n/a
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.
Oct 31 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a

John Salerno wrote:
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()?

Oct 31 '06 #2

P: n/a
At Tuesday 31/10/2006 18:01, John Salerno wrote:
>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
Oct 31 '06 #3

P: n/a
On Oct 31, 4:01 pm, John Salerno <johnj...@NOSPAMgmail.comwrote:
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.]

Nov 1 '06 #4

P: n/a
ge**********@gmail.com writes:
On Oct 31, 4:01 pm, John Salerno <johnj...@NOSPAMgmail.comwrote:
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 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?

--
\ "We have to go forth and crush every world view that doesn't |
`\ believe in tolerance and free speech." -- David Brin |
_o__) |
Ben Finney

Nov 1 '06 #5

P: n/a
On Tuesday 31 October 2006 16:01, John Salerno
wrote:
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
Nov 1 '06 #6

P: n/a

John Salerno wrote:
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

Nov 1 '06 #7

P: n/a
"wi******@hotmail.com" <ma**********@gmail.comwrites:
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.

--
\ "If you continue running Windows, your system may become |
`\ unstable." -- Microsoft, Windows 95 BSOD message |
_o__) |
Ben Finney

Nov 1 '06 #8

P: n/a

Ben Finney wrote:
"wi******@hotmail.com" <ma**********@gmail.comwrites:
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.
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
--
\ "If you continue running Windows, your system may become |
`\ unstable." -- Microsoft, Windows 95 BSOD message |
_o__) |
Ben Finney
Nov 1 '06 #9

P: n/a
"wi******@hotmail.com" <ma**********@gmail.comwrites:
Ben Finney wrote:
"wi******@hotmail.com" <ma**********@gmail.comwrites:
You could try to read the file, if that fails it doesn't exist:
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
Or you can simply use 'os.path.exists', as has been suggested several
times in this thread.

--
\ "The way to build large Python applications is to componentize |
`\ and loosely-couple the hell out of everything." -- Aahz |
_o__) |
Ben Finney

Nov 1 '06 #10

P: n/a
Ben Finney wrote:
"wi******@hotmail.com" <ma**********@gmail.comwrites:
Ben Finney wrote:
"wi******@hotmail.com" <ma**********@gmail.comwrites:
You could try to read the file, if that fails it doesn't exist:
>
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

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.

Nov 1 '06 #11

P: n/a


On Nov 1, 3:44 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
"witte...@hotmail.com" <martin.wi...@gmail.comwrites:
Ben Finney wrote:
"witte...@hotmail.com" <martin.wi...@gmail.comwrites:
You could try to read the file, if that fails it doesn't exist:
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 fileOr you can simply use 'os.path.exists', as has been suggested several
times in this thread.
os.path.exists() checks if a path exists, so a directory as argument
also returns True
--
\ "The way to build large Python applications is to componentize |
`\ and loosely-couple the hell out of everything." -- Aahz |
_o__) |
Ben Finney
Nov 1 '06 #12

P: n/a
wi******@hotmail.com wrote:
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>

Nov 1 '06 #13

P: n/a
On Thu, 02 Nov 2006 01:44:25 +1100, Ben Finney wrote:
"wi******@hotmail.com" <ma**********@gmail.comwrites:
>Ben Finney wrote:
"wi******@hotmail.com" <ma**********@gmail.comwrites:
You could try to read the file, if that fails it doesn't exist:

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

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.

--
Steven.

Nov 2 '06 #14

P: n/a
Steven D'Aprano wrote:
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>

Nov 2 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.