python 查看文件夾權限組和用OS模塊操作文件夾


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

 

來源http://jingyan.baidu.com/article/f3ad7d0ffe8b1409c2345b43.html
http://www.programcreek.com/python/index/478/win32security

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM