使用Python在共享Windows文件夹上创建权限

1 投票
2 回答
2780 浏览
提问于 2025-04-17 18:34

我在使用Python 3.3和pywin32库来共享一个文件夹。现在我想给这个文件夹添加权限。下面的代码并没有设置共享文件夹的权限。我想要添加一个特定的用户,并给予他读写的权限。

import win32net
import win32netcon

shinfo={}

shinfo['netname']='python test'
shinfo['type']=win32netcon.STYPE_DISKTREE
shinfo['remark']='data files'
shinfo['permissions']=0
shinfo['max_uses']=-1
shinfo['current_uses']=0
shinfo['path']='C:\\sharedfolder'
shinfo['passwd']=''
server='192.168.1.100'

win32net.NetShareAdd(server,2,shinfo)

2 个回答

3

你可以试着使用 win32security 模块。这个网站上有一个示例,教你如何用它来设置用户权限。

3

一个替代 win32security 模块的方法是选择一个更简单的工具 cacls,使用起来相对容易。你可以在这个链接查看相关信息:http://support.microsoft.com/kb/162786/en-us,比如:

from subprocess import *

proc = Popen("echo y|cacls filename /E /G BUILTIN\\Users:R", shell=True) 

proc.wait()

print "Child exited with",proc.returncode

这里的 echo y 是因为这个程序会问你“你确定吗?”这样的问题,挺烦人的。在 Windows 7 上,虽然 cacls 已经不推荐使用了(但仍然可以用),建议你用 icacls 来代替(或者使用资源工具包里的 xcalcs)。

当然,创建子进程来执行这些操作的效率没有直接调用 Win32 API 高。

撰写回答