Testing for a file in python?

Peter Schneider-Kamp petersc at stud.ntnu.no
Mon Jun 12 12:48:52 EDT 2000


"Thaddeus L. Olczyk" wrote:
> 
> stat doesn't work, it aborts if the file does not exist.

you could always catch the exception. But why not use
os.path?

>>> import os.path
>>> os.path.exists("python/")
1
>>> os.path.exists("spam/")
0
>>> os.path.exists("python/patches/submitted/array.pop-extend.patch")
1

If you only want a true for regular files have a look at isfile():

>>> os.path.isfile("python/")
0
>>> os.path.isfile("spam/")
0
>>> os.path.isfile("python/patches/submitted/array.pop-extend.patch")

for more info or other function look at:
http://python.org/doc/current/lib/module-os.path.html

good luck
Peter
--
Peter Schneider-Kamp          ++47-7388-7331
Herman Krags veg 51-11        mailto:peter at schneider-kamp.de
N-7050 Trondheim              http://schneider-kamp.de




More information about the Python-list mailing list