如何在 Windows 中设置文件夹权限?

编程入门 行业动态 更新时间:2024-10-24 12:33:44
本文介绍了如何在 Windows 中设置文件夹权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在创建用户 AD 帐户时,我正在使用 Python 创建一个新的个人文件夹.正在创建文件夹,但权限不正确.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,而不是创建一个空的 DACL,如下所示:

That 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 中设置文件夹权限?

本文发布于:2023-11-12 11:48:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   权限   如何在   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!