1

Note: I'm working on Python 3.x on Windows.

I've been working on this for quite a while, and I've found the solution on UNIX systems to be pretty easy.

os.stat("filename").st_uid

On Windows this just returns 0

I've found a solution that works sometimes

cmd = 'dir /q %s' % "filename"
usr = os.popen(cmd).read().split()[-11]
return usr

However, it doesn't always work, sometimes it'll error with the following:

IndexError: list index out of range

When it does work, it outputs

DOMAIN\USERNAME

During all of my searching over the past week or so, I've found several solutions that seem to work well in UNIX/LINUX but only the above solution works in Windows, and it's not consistent.

Pyrometheous
  • 65
  • 2
  • 14
  • 1
    I don't think that a file has to have an owner in windows. – zvone Dec 12 '19 at 18:27
  • Even if I could write a function that would return "owner" or "N/A" if the file doesn't have an owner, that would still work for my purposes. Although, I haven't run into a situation yet where I had to check the ownership of a file and it didn't have one. – Pyrometheous Dec 12 '19 at 18:30
  • 1
    The solution [here](https://stackoverflow.com/q/8086412/4739755) is pretty involved, but might be worth looking into. – b_c Dec 12 '19 at 18:30
  • You can easily have `"N/A"`, if that is what you want, with a simple `except IndexError: return "N/A"`... The main question is, can you, for the file which fails, see the owner in Windows Explorer? If yes, you should look for a solution, if not, accept that it has to fail. – zvone Dec 12 '19 at 18:32
  • For the ones that fail, I can manually go to the file, and it gives me the owner. For now, I just have it not populate the owner when it fails. So, yes, I'm looking for a solution. – Pyrometheous Dec 12 '19 at 18:37
  • @b_c I don't really completely understand how this works. I got it working by itself, however when I add everything to the larger project, I get the following error `ctypes.ArgumentError: argument 1: : wrong type`. I don't understand enough about what it's doing to troubleshoot it. The error is at `error = advapi32.GetNamedSecurityInfoW(filename, SE_FILE_OBJECT, request, ctypes.byref(pSD.pOwner), ctypes.byref(pSD.pGroup), ctypes.byref(pSD.pDacl), ctypes.byref(pSD.pSacl), ctypes.byref(pSD))` – Pyrometheous Dec 12 '19 at 20:14
  • Figured out how to get [this](https://stackoverflow.com/questions/8086412/howto-determine-file-owner-on-windows-using-python-without-pywin32) to work. My path wasn't a str, so I had to convert it first. I feel really stupid about that. Still, I feel like this is a lot of code to get the owner, but it works. – Pyrometheous Dec 12 '19 at 21:39
  • 1
    @Pyrometheous, that question required using ctypes. You can simplify the solution if PyWin32's win32security module is available. – Eryk Sun Dec 13 '19 at 03:15
  • Only (potential) downside to that is that it requires a third-party dependency. If you're interested in that approach though, the asked question in that link includes the PyWin32 method (also [here](http://timgolden.me.uk/python/win32_how_do_i/get-the-owner-of-a-file.html)), although the example is in python 2. – b_c Dec 13 '19 at 14:51

0 Answers0