最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

python查看文件夹权限组和用OS模块操作文件夹

来源:懂视网 责编:小采 时间:2020-11-27 14:24:49
文档

python查看文件夹权限组和用OS模块操作文件夹

python查看文件夹权限组和用OS模块操作文件夹:@建议操作server服务器文件夹时可以映射网络驱动盘import win32securityimport ntsecuritycon as conFILENAME = r'D:\tmp\acc_test' #文件夹路径sd = win32security.GetFileSecurity(FILENAME, win3
推荐度:
导读python查看文件夹权限组和用OS模块操作文件夹:@建议操作server服务器文件夹时可以映射网络驱动盘import win32securityimport ntsecuritycon as conFILENAME = r'D:\tmp\acc_test' #文件夹路径sd = win32security.GetFileSecurity(FILENAME, win3

@建议操作server服务器文件夹时可以映射网络驱动盘

import win32security
import ntsecuritycon as con

FILENAME = r'D:\tmp\acc_test' #文件夹路径

sd = win32security.GetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()

ace_count = dacl.GetAceCount() #显示具有文件夹权限用户(组)数量
print('Ace count:', ace_count)

for i in range(0, ace_count):
rev, access, usersid = dacl.GetAce(i)
user, group, type = win32security.LookupAccountSid('', usersid)
print('User: {}/{}'.format(group, user), rev, access)
User: user (0, 0) 1179817 #只有该文件夹,查看权限
User: admin (0, 0) 1245631 #只有该文件夹,修改权限
User: administrator (0, 3) 2032127 #此文件夹,子文件夹 全部权限

#删除文件夹权限
Now you are able to read old ACEs and deleting old ones is quite simple:
for i in range(0, ace_count):
dacl.DeleteAce(0)

#掉用add增加权限
And after that you can just add privileges by calling AddAccessAllowedAceEx() [MSDN]:
userx, domain, type = win32security.LookupAccountName ("", "your.user")
usery, domain, type = win32security.LookupAccountName ("", "other.user")

dacl.AddAccessAllowedAceEx(win32security.ACL_REVISION, 3, 2032127, userx) # 完全控制 @中间3代表权限会被后面文件夹继承 0:不会
dacl.AddAccessAllowedAceEx(win32security.ACL_REVISION, 3, 1179785, usery) # 只读权限

sd.SetSecurityDescriptorDacl(1, dacl, 0) #可能没有必要
win32security.SetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION, sd)
I've taken numbers 3, 2032127 and 1179785 from the listing in first half of the script (before running the script I've set up privileges in Explorer->Right Click->Properties->Security->Advanced):
#

#
#例子
# try:
# permit = '1179817'
# acegroup = 'Users'
# folder = 'Z:\\50.测试文件夹\\01.2'
# sd = win32security.GetFileSecurity(folder, win32security.DACL_SECURITY_INFORMATION)
# dacl = sd.GetSecurityDescriptorDacl()
# user1, domain, type = win32security.LookupAccountName('', acegroup)
#
# permisson = int(permit)
# dacl.AddAccessAllowedAceEx(win32security.ACL_REVISION, 0, permisson, user1) #中间的0 表示只有该文件夹 3表示此文件夹,子文件
# sd.SetSecurityDescriptorDacl(1, dacl, 0) # may not be necessary
# win32security.SetFileSecurity(folder, win32security.DACL_SECURITY_INFORMATION, sd)
# except Exception as e:
# print(e)

#python 的os模块操控文件夹
#import os
#import shutil

# try:
# os.mkdir(r'Z:\50.测试文件夹\01.2') #新建文件夹
# except Exception as e:
# print(e)

# try:
# os.rename(r'Z:\50.测试文件夹\01.2',r'Z:\50.测试文件夹\01.3') #重命名文件夹
# except Exception as e:
# print(e)

# try:
# shutil.rmtree(r'Z:/50.测试文件夹/01.3') ##删除名文件夹
# except Exception as e:
# print(e)
PS.调用cmd操控文件夹
http://jingyan.baidu.com/article/d3b74d64c853081f77e60929.html 下载安装pywin32
#创建文件夹
def createfolder(request):
folder=r'Z:/50.测试文件夹/01.3'
result={}
try:
#用wmi连接到远程服务器
pythoncom.CoInitialize()
conn = wmi.WMI(computer=‘192.168.0.1’, user='user', password='user')
cmd_callbat=r"cmd.exe /c mkdir %s" % folder
conn.Win32_Process.Create(CommandLine=cmd_callbat) #执行bat文件
result['isSuccess']=True
result['message']=' 添加成功'
except Exception as e:
print(e)
result['isSuccess'] = False
result['message'] = ' 添加失败'+e
response = HttpResponse()
response['Content-Type'] = "text/javascript"
response.write(json.dumps(result))
return response

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

python查看文件夹权限组和用OS模块操作文件夹

python查看文件夹权限组和用OS模块操作文件夹:@建议操作server服务器文件夹时可以映射网络驱动盘import win32securityimport ntsecuritycon as conFILENAME = r'D:\tmp\acc_test' #文件夹路径sd = win32security.GetFileSecurity(FILENAME, win3
推荐度:
标签: 文件 文件夹 目录
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top