python 获得 windows 系统里文件/文件夹的所有者

先安装 python3.5, 再安装 pywin32-227.win32-py3.5.exe (对应32位 python)或 pywin32-227.win-amd64-py3.5.exe(对应64位 python)

获得文件的所有者:

import win32api
import win32con
import win32security

# 获得登陆用户的名字
print "I am", win32api.GetUserNameEx(win32con.NameSamCompatible)


FILENAME = "temp.txt"
open(FILENAME, "r").close()  # 这里要写 "r",不能写 "w"

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)

打印结果:

I am 当前登陆用户名\当前登陆用户名
File owned by 文件的所有者所属的域\文件的所有者的用户名

获取文件夹的所有者:

import win32api
import win32con
import win32security

FILENAME = "d:\\test"

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

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

打印结果:
Dir owned by 文件夹的所有者所属的域\文件夹的所有者的用户名

-----------------------------------------------------------------------------

其他参考:

各个版本 pywin32 的下载地址:   https://github.com/mhammond/pywin32/releases

解决在Python中使用Win32api报错的问题,No module named win32api: 解决在Python中使用Win32api报错的问题,No module named win32api - 天道酬勤2016 - 博客园

这段 python 程序的英语说明: Tim Golden's Python Stuff: Get the owner of a file

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值