[Tutor] Re: Tutor digest, Vol 1 #1268 - 7 msgs

Blake.Garretson@dana.com Blake.Garretson@dana.com
Thu, 13 Dec 2001 12:57:40 -0500


>From: Kirk Bailey <deliberatus@my995internet.com>
>Subject: [Tutor] Amazing
>
>there is nothing I can find for a function or process or ANYTHING to
>determine if a file exists in a specified place. Something like
>exist(filename) really could be useful, but I cannot find any such!

I prefer to use the os.access() function.  It gives you more information
than os.path.exists() because it will tell you if it exists AND has certain
file permissions.  You just give it the filename and a permissions flag.

The valid permission flags are:

F_OK: existance flag
R_OK: readability flag
W_OK: writability flag
X_OK: executable flag

For instance:

###
>>>import os
>>>os.access("Filename.txt",R_OK)
1
###

This tells me that the file exists AND I have the permission to read it.
Likewise, this tells me that it exists and I can write to the file:

###
>>>import os
>>>os.access("Filename.txt",W_OK)
1
###

Hope this helps,
Blake Garretson