Python Forum
Get File Ownership under Windows
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get File Ownership under Windows
#1
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
Reply
#2
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?
Reply
#3
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"].
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
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?
Reply
#5
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
http://stackoverflow.com/questions/18306...-in-python
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#7
(Mar-13-2017, 09:26 PM)Ofnuts Wrote: http://stackoverflow.com/questions/18306...-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'.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  batch file for running python scipt in Windows shell MaartenRo 2 1,885 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  Using a .bat file to execute simple anaconda commands in Windows 10 gacg1 0 4,676 Nov-30-2020, 03:24 PM
Last Post: gacg1
  Q on file ownership ebolisa 2 2,143 Jan-02-2020, 05:44 PM
Last Post: ebolisa
  opening a file from a windows desktop folder- error opening file Charan007 1 2,977 Dec-06-2018, 11:50 AM
Last Post: buran
  python 2.7 access denied when writing a file to Windows kavise 2 11,313 Feb-13-2018, 08:30 PM
Last Post: kavise
  run .py file on Windows aidanikuz 4 4,636 Sep-19-2017, 02:33 PM
Last Post: aidanikuz
  Python 3.x Windows 7 registry file associations broken pixhellmann 2 5,064 Sep-12-2017, 09:14 PM
Last Post: Larz60+
  File access confusion python windows PickyBiker 0 3,360 May-08-2017, 05:36 PM
Last Post: PickyBiker
  Windows: Put File automatically under Cursor for Drag & Drop sym 1 3,477 Jan-13-2017, 04:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020