Windows: get owner and group of a file

Discussion in 'Python' started by kai rosenthal, Dec 6, 2006.

  1. 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
    kai rosenthal, Dec 6, 2006
    #1
    1. Advertising

  2. kai rosenthal 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').


    Are you by any chance running cygwin? That comes with ls, but windows
    doesn't. So you need to add the appropriate cygwin binary dirs to you path.
    But you probably won't want that anyway, as thus your code would only run
    on a machine with cygwin installed.

    > 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.


    I'm not sure if stat-calls are fully supported on windows - the windows
    rights management is considerably different from unixish ones.

    From the docs:

    """
    For backward compatibility, the return value of stat() is also accessible as
    a tuple of at least 10 integers giving the most important (and portable)
    members of the stat structure, in the order st_mode, st_ino, st_dev,
    st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime. More items
    may be added at the end by some implementations. The standard module stat
    defines functions and constants that are useful for extracting information
    from a stat structure. (On Windows, some items are filled with dummy
    values.)
    """

    Note the last sentence. You should try and look what win32 has to offer.

    Diez
    Diez B. Roggisch, Dec 6, 2006
    #2
    1. Advertising

  3. Tim Golden wrote:

    > Wow. Python 2.2. No extensions. Not even popen (). You don't
    > want much, do you? I *think* the answer is that you can't.


    does the "group" concept even exist on Windows ? cannot recall I've
    ever seen "ls -l" print anything but "everyone"...

    </F>
    Fredrik Lundh, Dec 6, 2006
    #3
  4. kai rosenthal

    Tim Chase Guest

    >> with ls -l on windows I get
    >> -rw-r--r-- 1 500 everyone 320 Nov 09 09:35 myfile

    >
    > Are you by any chance running cygwin? That comes with ls, but
    > windows doesn't.


    Another alternative might be mounting their Windows-formatted
    drive from within a *nix-like OS. These permissions are usually
    set via the /etc/fstab mounting options for the drive. I don't
    remember what the defaults are, as I have mine set up to take a
    forced set of uid/gid for users/groups specific to my system.
    Because at least FAT32 doesn't have this idea of groups (mapping
    to NTFS is an entirely different ball of wax), you can specify a
    default mask for everything or specify the directory mask
    separately from the file mask.

    man mount

    will give more details on such goods.

    -tkc
    Tim Chase, Dec 6, 2006
    #4
  5. kai rosenthal

    Duncan Booth Guest

    Fredrik Lundh <> wrote:

    > Tim Golden wrote:
    >
    >> Wow. Python 2.2. No extensions. Not even popen (). You don't
    >> want much, do you? I *think* the answer is that you can't.

    >
    > does the "group" concept even exist on Windows ? cannot recall I've
    > ever seen "ls -l" print anything but "everyone"...


    Domain users have a 'primary group'. Actually, contrary to what I said in
    the previous post I'm not sure that files have the concept. It may just be
    users and the actual group permissions then get stored on the file. If so
    and if you assigned any other groups permission on the file you may not be
    able to distinguish which is the original group for the file and which was
    added later.
    Duncan Booth, Dec 6, 2006
    #5
  6. kai rosenthal

    Duncan Booth Guest

    "kai rosenthal" <> 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
    Duncan Booth, Dec 6, 2006
    #6
  7. kai rosenthal

    MRAB Guest

    kai rosenthal 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
    >

    If you can't use os.popen('ls -l'), can you instead use os.system('ls
    -l >C:\\Temp\\result.txt') and then parse result.txt?
    MRAB, Dec 7, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jojo
    Replies:
    20
    Views:
    1,109
  2. Tzap
    Replies:
    1
    Views:
    898
    Joe Attardi
    Mar 1, 2006
  3. Thibault Ketterer

    Get file owner in Windows

    Thibault Ketterer, Jul 23, 2007, in forum: Python
    Replies:
    0
    Views:
    358
    Thibault Ketterer
    Jul 23, 2007
  4. JK
    Replies:
    2
    Views:
    448
    james
    Dec 18, 2007
  5. Aqua

    Windows 2000/XP: Owner of a File

    Aqua, Sep 20, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    108
    Dr.Ruud
    Sep 21, 2006
Loading...

Share This Page