Python Forum
Get File Ownership under Windows - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Get File Ownership under Windows (/thread-2400.html)



Get File Ownership under Windows - newatpython - Mar-13-2017

All, 

New to programming here. First project for work. 

problem: I am looping through and moving some files around based on owner of the file. I just want to day "if owner is Joe then move to Joe folder. If Jane, jane folder." 
platform: Python 2.7 windows 2008r2. 

Hoping someone has a code snipplet to help me out there? 

thanks
-Daniel


RE: Get File Ownership under Windows - Larz60+ - Mar-13-2017

I wouldn't mind knowing that myself. Microsoft in their typical way, has taken a perfectly
usable well thought out file security system that was created and proven over a 40+ year
period.

Why should you make someone learn:
/pvr [u::rwx,g::r-x,o::r-x/u::rwx,u:sue:rwx,g::r-x,m::rwx,o::r-x]
When you can make a simple checkbox system that any manager can control, especially
those who who have never had a single hour of security training, and show all of the
software people who's the boss!

This is probably why the world knows everybody's business, because no one
has a clue of what their doing!

Ok, I'm done. What was the question?


RE: Get File Ownership under Windows - wavic - Mar-13-2017

Hello!
See os and os.path modules.

import os
import os.path

work_dir = "c:/path/"
new_dir = "path"
user = os.getlogin()

for root, dirs, files in os.walk(work_dir):
    if files:
        for f in files:
            if os.stat(f).st_uid == user:
                os.path.rename(f, os.path.join(os.path.abspath(new_dir), f))
Something like that.

Well, you can replace os.getlogin() with the actual owner name. Or with os.environ["USER"].


RE: Get File Ownership under Windows - zivoni - Mar-13-2017

On unix/linux wavic's code would work (with user = pwd.getpwuid(uid).pw_name to translate uid to username), but with windows there might be some problems to get user/owner.

Quick googling revealed that for some combinations of python / windows os.stat gives dummy values and even if it doesnt, I have no idea how to translate uid to username (unless you hardcode that translation). Perhaps pywin32 - python extensions for windows could be used?


RE: Get File Ownership under Windows - wavic - Mar-13-2017

I didn't dig at all. Just to point in some direction. I don't use Windows. It seems like many people still use it every day.  Think
Perhaps ti will worth to install it on my machine. For testing purposes.


RE: Get File Ownership under Windows - Ofnuts - Mar-13-2017

http://stackoverflow.com/questions/1830618/how-to-find-the-owner-of-a-file-or-directory-in-python


RE: Get File Ownership under Windows - sparkz_alot - Mar-24-2017

(Mar-13-2017, 09:26 PM)Ofnuts Wrote: http://stackoverflow.com/questions/1830618/how-to-find-the-owner-of-a-file-or-directory-in-python

I'm going to give this half a thumbs up as it only addresses half the problem, the other half is actually moving the files.  I believe (not 100% sure) that you have to be the user (owner of the file) and even then only to a directory for which you have 'write' privileges or greater, or you need administrative privileges (for linux, think sudo) or possibly even be logged in as Administrator (for linux, think root).  Note that administrative privileges is not the same as Administrator, which is why, by default, Windows disables the Administrator, much like many Linux versions hide the 'root' login.  An example, on my Windows 10 machine, I have administrative permissions, yet I am not allowed to write to the 'hosts' file, for that, I have to login as 'Administrator'.