faqts : Computers : Programming : Languages : Python : Common Problems : Files

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

254 of 260 people (98%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How do I check to see if a file exists?

Jun 13th, 2000 20:12
unknown unknown, Peter Schneider-Kamp


To check if a file exists use:

>>> 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