Solved

Can I use Python to retrieve a file owner in a Windows environment?

Posted on 2014-01-09
9
1,476 Views
Last Modified: 2014-01-14
Using Python, I would like to iterate through a series of files, obtaining the owner name (not the UID) from each of them.  In the Unix environment, the pwd module allows you to do this.  Is there anything comparable in the Windows environment?
0
Comment
Question by:cshore12
  • 4
  • 3
  • 2
9 Comments
 
LVL 19

Assisted Solution

by:Thommy
Thommy earned 250 total points
ID: 39770295
In windows you can use PowerShell to determine owner of a file or multiple files...



PowerShell-Command for a specific file
Get-Acl C:\Scripts\Test.txt

Result:
Path                          Owner                         Access
----                          -----                         ------
Test.txt                      FABRIKAM\kenmyer              BUILTIN\Administrators Allow  FullCo...

PowerShell-Command for multiple files
Get-Acl C:\Scripts\*.*

Result:
Path                          Owner                         Access
----                          -----                         ------
Example.txt          FABRIKAM\kenmyer              BUILTIN\Administrators Allow  FullCo...
Test.txt                  FABRIKAM\pilarackerman    BUILTIN\Administrators Allow  FullCo...
Trial.txt                 FABRIKAM\kenmyer              BUILTIN\Administrators Allow  FullCo...
0
 

Author Comment

by:cshore12
ID: 39771017
Yes, that will do it -- thank you!

I'll be writing a larger script that uses some standard logic and utilities, seach as iterating through folders, searching, matching strings, etc.  So far I've only read a small part of the tutorial; can I assume that PS is capable of such functionality?
0
 
LVL 19

Expert Comment

by:Thommy
ID: 39771069
Powershell as its name already says is very powerful.

You can loop through directory structures...
ForEach Loop through a directory

String operations are also no problem...
The String’s the Thing
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 250 total points
ID: 39774246
If you want to use python, you can use the functions (GetFileSecurity and LookupAccountSid) from the win32security library (included with pywin32) to retrieve the owner information:

import win32security

def GetOwner(filename):
    f = win32security.GetFileSecurity(filename, win32security.OWNER_SECURITY_INFORMATION)
    (username, domain, sid_name_use) =  win32security.LookupAccountSid(None, f.GetSecurityDescriptorOwner())
    return username

print GetOwner(r"c:\somefile.txt")

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:cshore12
ID: 39774970
clockwatcher-

I tried your code, but was told that the win32security module doesn't support the GetFileSecurity method.  I'm running Py 2.7.2, and also downloaded the pywin32 executable.
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 39775321
Can you post your exact error message?  

The win32security module definitely supports the GetFileSecurity method:

   http://docs.activestate.com/activepython/2.7/pywin32/win32security__GetFileSecurity_meth.html

I've tested my code on Windows 7 and 8 and works fine under various python versions (including 2.7).  What OS are you on?
0
 

Author Comment

by:cshore12
ID: 39776874
clockwatcher:

I'm using Win 7 Enterprise, sp1.  Here's the traceback:

Traceback (most recent call last):
  File "C:\Other\win32security.py", line 1, in <module>
    import win32security
  File "C:\Other\win32security.py", line 8, in <module>
    print GetOwner(r"c:\Other\CHICKEN_STUFF.doc")
  File "C:\Other\win32security.py", line 4, in GetOwner
    f = win32security.GetFileSecurity(filename, win32security.OWNER_SECURITY_INFORMATION)
AttributeError: 'module' object has no attribute 'GetFileSecurity'
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 39778342
It appears you named your program win32security.py.  You can't do that and then load a module with the same name.  If you name your program, win32security.py and then do a:

  import win32security

From within that same program, you're loading your own program (not the win32security module that is installed with pywin32) as a module.

Long story short, rename your file something other than win32security.py so it doesn't conflict with and override the standard module.
0
 

Author Comment

by:cshore12
ID: 39779262
Yes, that was stupid of me!  I renamed the file, and the win32security module works great now.

I'm going to award each of you an equal share.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now