@建議操作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#User: user (0#User: user , 0) 1179817 #只有該資料夾,檢視權限
User: admin (0, 0) 1245631 #只有該資料夾,修改權限
User: administrator (0, 3) 2032127 修改權限
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):
#掉用add增加權限
And after that you can just add privileges by calling AddAccessAllowedAceEx() [MSDN]:
userx, domain, type = win32security.LookupAccountName ("", "your.user")
##
dacl.AddAccessAllowedAceEx(win32security. ACL_REVISION, 3, 2032127, userx) # 完全控制@中間3代表權限會被後面資料夾繼承0:不會
dacl.AddAccessAllowedAceEx(win32security.ACL_REVISION, 3, 1179785, useruser) # 只讀權限
win32security.SetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION, sd)
I've taken numbers 3, 203217787572727598989999999999990000000 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'
#110 sd = win32security.GetFileSecurity(folder, win32security.DACL_SECURITY_INFORMATION)
# dacl = sd.GetSecurityDescriptorDacl()
# user1, domain, 132d permisson = int(permit)
# dacl.AddAccessAllowedAceEx(win32security.ACL_REVISION, 0, permisson, user1) #中間的0 表示只有該資料夾3表示此資料夾,子檔案#oo #po# , dacl, 0) # may not be necessary
# win32security.SetFileSecurity(folder, win32security.DACL_SECURITY_INFORMATION, sd)
# except#ception as 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):### #建立資料夾###def createfolder(request):### folder=r'Z: /50 .測試資料夾/01.3'### result={}### try:### #使用wmi連接至遠端伺服器### pythoncom.CoInitialize()#18 .0.1', user='user', password='user')### cmd_callbat=r"cmd.exe /c mkdir %s" % folder### all conn.Win32_ProcessmL.Winrebat文件### result['isSuccess']=True#### result['isSuccess']=True#### result['message']=' 新增成功'### except 選擇= False### result['message'] = ' 新增失敗'+e### result['message'] = ' 新增失敗'+e### result['message'] = ' 新增失敗'+e### result['message'] = ' 新增失敗'+e### response = HttpResponse()### response['Content-Type'] = "text/javascript"##reso o. .dumps(result))### return response####### #####
以上是python查看資料夾權限群組和用OS模組操作資料夾的詳細內容。更多資訊請關注PHP中文網其他相關文章!