在windows系统上使用python获取文件的所有者

vfh0ocws  于 2023-02-05  发布在  Windows
关注(0)|答案(2)|浏览(157)

我想在Windows系统上的python3脚本中提取文件的所有者。
当我运行:os.path.expanduser(fp)os.stat(fp).st_uid时,我收到了所有讨论文件的0
有人知道如何在windows上使用python提取文件所有者的id或名称吗?
谢谢-我-

fwzugrvs

fwzugrvs1#

import win32api
import win32con
import win32security

FILENAME = "temp.txt"
open (FILENAME, "w").close ()

print "I am", win32api.GetUserNameEx (win32con.NameSamCompatible)

sd = win32security.GetFileSecurity (FILENAME, win32security.OWNER_SECURITY_INFORMATION)
owner_sid = sd.GetSecurityDescriptorOwner ()
name, domain, type = win32security.LookupAccountSid (None, owner_sid)

print "File owned by %s\\%s" % (domain, name)
tkclm6bt

tkclm6bt2#

您需要安装pywin32软件包:

$ pip install pywin32

要获取文件的idname of an owner,可以使用以下代码:

import win32security

sd = win32security.GetFileSecurity(file, win32security.OWNER_SECURITY_INFORMATION)
owner_sid = sd.GetSecurityDescriptorOwner()
name, domain, type = win32security.LookupAccountSid(None, owner_sid)
print(name)

相关问题