A little disappointed so far
Aahz
aahz at pythoncraft.com
Sun May 18 21:05:10 EDT 2003
In article <PRVxa.118$573.48 at news-binary.blueyonder.co.uk>,
Graham Nicholls <graham at rockcons.co.uk> wrote:
>
>Is there an equivalent of [ -f $filename ] to test for the existence of
>filename in Python? Things like this seem essential for a shell tool
>language.
Sure:
os.path.isfile(filename)
But Alex will probably trundle by in a moment to tell you that in many
cases, you don't want to do that. Python's powerful exception handling
system means that most of the time you just do:
try:
f = file(filename)
except IOError:
<handle exception>
Thing is, no matter how much you test, there's always the possibility of
a race condition that prevents you from opening a file. I'll note that
since you're mostly doing shell script equivalents, it's more likely
that you'll want to do the tests (because you want to know whether
something is a file or a directory).
One of the great things about the Python docs is that once you know
about os and os.path, you know where to look for information.
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles: boring syntax, unsurprising semantics,
few automatic coercions, etc etc. But that's one of the things I like
about it." --Tim Peters on Python, 16 Sep 93
More information about the Python-list
mailing list