设置文件夹权限的Windows使用Python在 [英] Setting folder permissions in Windows using Python

查看:1757
本文介绍了设置文件夹权限的Windows使用Python在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python创建了一个用户AD帐户何时创建一个新的个人文件夹。正在创建的文件夹,但权限是不正确的。可以的Python将用户添加到新创建的文件夹,并改变他们的权限?我不知道从哪里开始编码这一点。

I'm using Python to create a new personal folder when a users AD account is created. The folder is being created but the permissions are not correct. Can Python add the user to the newly created folder and change their permissions? I'm not sure where to begin coding this.

推荐答案

您想要的 win32security 模块,这是对的pywin32 。这里是做你想要做的事情有些例子

You want the win32security module, which is a part of pywin32. Here's an example of doing the sort of thing you want to do.

这个例子的文件创建一个新的DACL,并取代旧的,但很容易修改现有之一;所有你需要做的就是从安全描述符中的现有DACL而不是建立空单,像这样:

The example creates a new DACL for the file and replaces the old one, but it's easy to modify the existing one; all you need to do is get the existing DACL from the security descriptor instead of creating an empty one, like so:

import win32security
import ntsecuritycon as con

FILENAME = "whatever"

userx, domain, type = win32security.LookupAccountName ("", "User X")
usery, domain, type = win32security.LookupAccountName ("", "User Y")

sd = win32security.GetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()   # instead of dacl = win32security.ACL()

dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_GENERIC_READ | con.FILE_GENERIC_WRITE, userx)
dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_ALL_ACCESS, usery)

sd.SetSecurityDescriptorDacl(1, dacl, 0)   # may not be necessary
win32security.SetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION, sd)

这篇关于设置文件夹权限的Windows使用Python在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆