1

I'm using this code in python 3.3 with the pywin32 library to share a folder. How can I now add pemissions to the folder? The following code sets no permissions to the shared folder. I want to add a specific user as read/write

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)
user391986
  • 29,536
  • 39
  • 126
  • 205

2 Answers2

3

you should try and use win32security module. This site shows an example of using it to set user permissions

cyberbemon
  • 3,000
  • 11
  • 37
  • 62
  • What is the benefit of using the win32security module opposed to icacls? – TechDude Sep 07 '14 at 22:58
  • 1
    @TechDude: Efficiency: calling a child process, the shell and cacls, will always be less efficient than using the API direct. If you are changing large numbers of file permissions regularly then you will notice the difference. – cdarke Sep 08 '14 at 12:50
3

An alternative to the win32security module is to wimp out and use the cacls program, which is rather easier to use, see at http://support.microsoft.com/kb/162786/en-us for example :

from subprocess import *

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

proc.wait()

print "Child exited with",proc.returncode

The echo y is because this stupid program asks an "are you sure?" question. On windows 7, cacls is deprecated (but still works), use icacls instead (or xcalcs from the resource kit).

Of course creating child processes to do this will not be as efficient as calling the Win32 API.

cdarke
  • 42,728
  • 8
  • 80
  • 84