[Tutor] how to know if a file exists

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Jan 4 06:20:45 CET 2007



On Thu, 4 Jan 2007, Andre Roberge wrote:

>> > i am writing an app for linux. what command would be easiest to test
>> > and see if a certain file exist ?
>> > i was going to do something like this
>> > try:
>> >     file = open('/path/to/file', 'rb')
>> >     return True
>> > except:
>> >     return False
>> You should except IOError here, just to be clear and such.
>> >
>> > but i thought that there would be an easier way.
>> The os module has some function for checking if files exist, I think.
>
> Yes, check out access():
> http://docs.python.org/lib/os-file-dir.html

Hi Andrey,

os.path.exists() may also be useful here:

     http://www.python.org/doc/lib/module-os.path.html#l2h-2163


More information about the Tutor mailing list