Solved

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

Posted on 2014-01-09
9
1,733 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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
 

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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to use the Hootsuite Dashboard.
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…

809 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