Solved

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

Posted on 2014-01-09
9
1,821 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.

763 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