reading a file

Alex Martelli aleaxit at yahoo.com
Wed Aug 30 09:32:31 EDT 2000


"Janos Blazi" <jblazi at vipsurf.de> wrote in message
news:39ad0281$1_4 at news.newsfeeds.com...
> I'd like to read a file *if it exists* (and do nothing if the file does
not
> exist). Which is the simplest way of doing this? I am doing this with
> try-except at the moment but it is a bit ugly, especially as a do not have
> anything to do in the except clause.

Are you sure it's OK to die with a horrid crash if the file DOES
exist but is not readable for any of N other reasons...?  Permission
problems, locking/sharing-violation issues, special nonreadable
files, etc, etc...?  99% of the time, when somebody says "read the
file if it exists" he or she is truly implying something different,
i.e., "read if it exists AND is readable", which simplifies to
"read if it's readable".

If you're really keen to get exceptions for all other causes of
non-readability besides non-existence, you can do that:

import os

def readIfExists(filename):
    if os.access(filename,os.F_OK):
        return open(filename,'r').read()
    else:
        return ''    # non-existence==emptiness

...assuming that doesn't get you into some "race condition", since
other processes might be playing funny tricks between the file
existence test and the attempt to open it for reading.


Personally, I find the following code, which is much more solid
and likely to have the expected functionality, not ugly at all:

def readIfPossible(filename):
    try:
        return open(filename,'r').read()
    except:
        return ''  # unreadability==emptiness


This is the "it's easier to ask forgiveness than permission"
design pattern, thus named by a quote from Admiral Grace Murray
Hopper (she was talking about dealing with bureaucracy, but
it works just as well in computer terms -- and, she did invent
Cobol, after all:-).  Rather than trying to ensure all pre-
conditions are met (it's hard to ensure everything has been
tested, easy to get into race-conditions, etc), just attempt
the operation in a context that will ensure an Exception if
some precondition is violated, and of course handle that
exception if and when it comes.  Python lends itself quite
well to this, although of course it's quite as applicable in
other languages/systems with exception-handling, too.

Still, if "asking permission" IS really what's needed, then
Python does let you do that.  But, IMHO, it should be done
only if and when the user's specs are really adamant about it
(e.g. "the program reads the foo.bar file at startup to restore
the last-checkpointed state; it's ok if foo.bar does not exist,
the program will then start the simulation from scratch; but,
if foobar exists and is not readable for any other reason, the
program must signal the error and exit immediately" -- one
does occasionally get specs of this kind, and once in a blue
moon they might even make some sense!-).


Alex






More information about the Python-list mailing list