How does Python handle probing to see if a file already exist s?

Lemniscate d_blade8 at hotmail.com
Tue Nov 5 17:58:20 EST 2002


Hi.  Before I start, I personally like Gustavo's solution.  However,
the os.path module has a ton of tools that you may want to get
familiar with currently.  Plus, there could be "issues" depending on
your code.  For example, running a directory under G.'s code:

> if os.access("some-filename", os.W_OK):
>    # neat, I can write to the file.
>    ...

will attempt to let you write to a directory like it is a file.  For
example, "Book Source" is a directory on my computer:

>>> os.access('Books Source', os.W_OK)
1
>>> # It's writable, let's try to write to it like a file
>>> file("Books Source", 'w').write("Nothing doing, here's an
error...")
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
IOError: [Errno 13] Permission denied: 'Books Source'
>>> # so, W_OK is yes, but you can't write to it

So, you see, you *may* need to do some checking.  Maybe as so...

>>> import os
>>> import os.path
>>> Possible_name = "Books Source"
>>> os.access(Possible_name, os.W_OK) and
os.path.isfile(Possible_name)
0
>>> os.access(Possible_name, os.W_OK) and os.path.isdir(Possible_name)
1
>>> # ahh, it's a dir, not a file

Just a couple of ideas so that you don't get stuck.

Chris






sismex01 at hebmex.com wrote in message news:<mailman.1036508171.27010.python-list at python.org>...
> > From: Christopher R. Culver [mailto:Kricxjo.neniuspamajxo at yahoo.com]
> > 
> > Hello all,
> > 
> > I've just begun learning Python after already getting pretty 
> > proficient in C. However, I'm having a problem with a simple
> > copying program. Ideally, the program would check to see if
> > the destination already exists and prompt the user. In this
> > this would be as easy as saying:
> > 
> > probe_destination = fopen(/usr/bin/foo, 'r')
> > if (probe_destination)
> > {
> > some code here;
> > }
> > 
> > However, translating that directly into Python doesn't work because
> > if Python can't find a file to open, it doesn't just return 0, it
> > gives an error and exits. How can I write this functionality in Python 
> > so that it returns 0 and continues, or does Python have a totally 
> > different way of handling this problem than C?
> > 
> > Christopher Culver
> >
> 
> Others have talked about the goodness of using try/except, I'll
> say to use os.access(), which is precisely for what you're asking,
> if you can read or write a file.
> 
> -- check if writeable --
> 
> import os
> if os.access("some-filename", os.W_OK):
>    # neat, I can write to the file.
>    ...
> 
> -- check if readable --
> 
> import os
> if os.access("some-filename", os.R_OK):
>    # OK, I can read from the file.
>    ...
> 
> -- check if executable --
> 
> import os
> if os.access("some-filename", os.X_OK):
>    # OK, I can execute this file.
>    ...
> 
> -- check existance --
> 
> import os
> if os.access("some-filename", os.F_OK):
>    # File exists, but I don't know what I can do with it.
>    ...
> 
> 
> You can do a bitwise OR on the flags R_OK, W_OK and X_OK to
> check combined permissions, R_OK|W_OK would check for read
> and write permission on the same file.
> 
> Exception handling is neat and all, but sometimes it just isn't
> the right way to only check for some condition.  I mean, you
> don't blindly walk into a door to see if it's locked or not.
> 
> :-)
> 
> -gustavo



More information about the Python-list mailing list