登录  /  注册

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

高洛峰
发布: 2017-03-27 16:26:31
原创
2154人浏览过

@建议操作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

 

 

以上就是python查看文件夹权限组和用OS模块操作文件夹 的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号