Understanding CHMOD

Fuzzyman michael at foord.net
Fri Feb 13 16:55:51 CET 2004


[snip..]
> 
> These are the basic access permissions.
> 
> S_IRGRP
> S_IROTH
> S_IRUSR
> 
> S_IWGRP
> S_IWOTH
> S_IWUSR
> 
> S_IXGRP
> S_IXOTH
> S_IXUSR
> 
> There are some shortcut (confusing IMHO) entries:
> 
> S_IEXEC  = S_IXUSR
> S_IWRITE = S_IWUSR
> S_IREAD  = S_IRUSR
> 
> S_IRWXG  = stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
> S_IRWXO  = stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH
> S_IRWXU  = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
> 
> The rest are "extended" attribute bits
> and file type bits.
> 
> Here's a simplified ls access listing in python:
> 
> #!/usr/bin/env python
> 
> import sys
> import stat
> import os
> 
> filename=sys.argv[1]
> mode=stat.S_IMODE(os.lstat(filename)[stat.ST_MODE])
> perms="-"
> for who in "USR", "GRP", "OTH":
>      for what in "R", "W", "X":
>          if mode & getattr(stat,"S_I"+what+who):
>              perms=perms+what.lower()
>          else:
>              perms=perms+"-"
> print perms + " " + filename

Thanks for your help !
I always find it slightly surprising when I get a reply that actually
answers the question ;-)

Fuzzy

http://www.voidspace.org.uk/atlantibots/pyhtonutils.html



More information about the Python-list mailing list