Python: View folder permission groups and use the OS module to operate folders.
@ We recommend that you map the network drive disk when operating the server folder.
Import win32security
Import ntsecuritycon as con
FILENAME = r 'd: \ tmp \ acc_test # folder path
Sd = win32security. GetFileSecurity (FILENAME, win32security. DACL_SECURITY_INFORMATION)
Dacl = sd. GetSecurityDescriptorDacl ()
Ace_count = dacl. GetAceCount () # display the number of users (groups) with folder Permissions
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 # Only this folder can be viewed.
User: admin (0, 0) 1245631 # Only this folder can be modified.
User: administrator (0, 3) 2032127 # All permissions for this folder
# Delete folder Permissions
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)
# Use add to add permissions
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) # Full control @ 中3 3 indicates that the permission will be inherited by the following folder 0: No
Dacl. AddAccessAllowedAceEx (win32security. ACL_REVISION, 3, 1179785, usery) # Read-Only permission
Sd. SetSecurityDescriptorDacl (1, dacl, 0) # unnecessary
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 ):
#
#
# Example
# Try:
# Permit = '000000'
# Acegroup = 'users'
# Folder = 'z: \ 50. Test folder \ 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 indicates that only the folder 3 indicates this folder, and the sub-File
# Sd. SetSecurityDescriptorDacl (1, dacl, 0) # may not be necessary
# Win32security. SetFileSecurity (folder, win32security. DACL_SECURITY_INFORMATION, sd)
# Failed t Exception as e:
# Print (e)
# Using the python OS module to manipulate folders
# Import OS
# Import shutil
# Try:
# OS. mkdir (r'z: \ 50. Test Folder \ 01.2 ') # create a folder
# Failed t Exception as e:
# Print (e)
# Try:
# OS. rename (r'z: \ 50. Test Folder \ 01.2 ', r'z: \ 50. Test Folder \ 01.3') # rename the folder
# Failed t Exception as e:
# Print (e)
# Try:
# Shutil. rmtree (r'z:/50. Test Folder/100') # delete the name folder
# Failed t Exception as e:
# Print (e)
PS. Call cmd to manipulate folders
Install pywin32 http://jingyan.baidu.com/article/d3b74d64c853081f77e60929.html download
# Creating folders
Def createfolder (request ):
Folder = r 'z:/50. Test folder/123456'
Result = {}
Try:
# Use wmi to connect to a remote server
Pythoncom. CoInitialize ()
Conn = wmi. WMI (computer = '192. 168.0.1 ', user = 'user', password = 'user ')
Export _callbat = r "cmd.exe/c mkdir % s" % folder
Conn. Win32_Process.Create (CommandLine = cmd_callbat) # execute the bat file
Result ['issuccess'] = True
Result ['message'] = 'added successfully'
Failed t Exception as e:
Print (e)
Result ['issuccess'] = False
Result ['message'] = 'add failed' + e
Response = HttpResponse ()
Response ['content-type'] = "text/javascript"
Response. write (json. dumps (result ))
Return response
Source http://jingyan.baidu.com/article/f3ad7d0ffe8b1409c2345b43.html
Http://www.programcreek.com/python/index/478/win32security