Windows: get owner and group of a file

Duncan Booth duncan.booth at invalid.invalid
Wed Dec 6 09:35:14 EST 2006


"kai rosenthal" <kairosenthal at tiscali.de> wrote:

> Hello,
> 
> with ls -l on windows I get
> -rw-r--r-- 1 500 everyone 320 Nov 09 09:35 myfile
> 
> How can I get on windows with a standard python 2.2 (without windows
> extensions) the information "500" and "everyone" (owner and group)?
> Also I cannot use popen('ls -l').
> 
> With
> import stat
> stat_info = os.lstat(myfile)
> owner = "%-8s" % stat_info.st_uid
> group = "%-8s" % stat_info.st_gid
> I get 0 for owner and group.
> 
> Thanks for your hints, Kai
> 

You can get the owner by doing os.popen('dir /q') and parsing the output, 
but it is a string not a number (which I guess is why stat/lstat can't 
return a value). Internally the ownber and primary group are stored as 
security identifier (SID) values: a SID is a variable length structure.

Running the CACLS command on a file will give you the full permission 
settings for that file. They are a lot more complex than the simple rwx 
settings from unix. e.g.

C:\temp>cacls t.py
C:\temp\t.py BUILTIN\Administrators:F
             NT AUTHORITY\SYSTEM:F
             MYPC\Duncan:F
             BUILTIN\Users:R


C:\temp>cacls .
C:\temp BUILTIN\Administrators:F
        BUILTIN\Administrators:(OI)(CI)(IO)F
        NT AUTHORITY\SYSTEM:F
        NT AUTHORITY\SYSTEM:(OI)(CI)(IO)F
        MYPC\Duncan:F
        CREATOR OWNER:(OI)(CI)(IO)F
        BUILTIN\Users:R
        BUILTIN\Users:(OI)(CI)(IO)(special access:)
                                  GENERIC_READ
                                  GENERIC_EXECUTE

        BUILTIN\Users:(CI)(special access:)
                          FILE_APPEND_DATA

        BUILTIN\Users:(CI)(special access:)
                          FILE_WRITE_DATA

So far as I know, to get the primary group for a file you will need to call 
the win32 function GetSecurityInfo asking for the 
GROUP_SECURITY_INFORMATION. This will give you an appropriate security 
descriptor. See http://msdn2.microsoft.com/en-us/library/aa379561.aspx




More information about the Python-list mailing list