Checking File permissions

Tal Einat taleinat at gmail.com
Fri Jul 21 08:46:46 EDT 2006


Anoop wrote:
> Hi All
>
> Please tell me how to check the existence of a file and the read
> permission to the file using python script
>
> Thanks for ur inputs
>
> Anoop

os.access(path, mode) does just this, check it out.

Cross-platform-ness isn't a problem, the docs say it is available for
Windows, Unix and Mac, and I've used it without a hitch on Win2k, WinXP
and various Linux and Unix platforms.

>>> help(os.access)
Help on built-in function access in module nt:

access(...)
    access(path, mode) -> 1 if granted, 0 otherwise

    Use the real uid/gid to test for access to a path.  Note that most
    operations will use the effective uid/gid, therefore this routine
can
    be used in a suid/sgid environment to test if the invoking user has
the
    specified access to the path.  The mode argument can be F_OK to
test
    existence, or the inclusive-OR of R_OK, W_OK, and X_OK.




More information about the Python-list mailing list