Tags: file, get-rw-r-r-, group, myfilehow, nov, owner, programming, python, standard, windows

Windows: get owner and group of a file

On Programmer » Python

9,023 words with 6 Comments; publish: Fri, 04 Jan 2008 10:17:00 GMT; (20046.88, « »)


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


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

All Comments

Leave a comment...

    • kai rosenthal wrote:

      === Original Words ===


      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



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


      #1; Fri, 04 Jan 2008 10:19:00 GMT
    • Tim Golden wrote:

      === Original Words ===

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


      #2; Fri, 04 Jan 2008 10:20:00 GMT
    • >with ls -l on windows I get
      === Original Words ===

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


#3; Fri, 04 Jan 2008 10:21:00 GMT
  • 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.

    #4; Fri, 04 Jan 2008 10:22:00 GMT