@建议操作server服务器文件夹时可以映射网络驱动盘
import win32securityimport 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
以上就是python查看文件夹权限组和用os模块操作文件夹 的详细内容,更多请关注 第一php社区 其它相关文章!