0

My python script creates a folder, one for each user. I want to make permissions/grant privileges where only one particular user can have access to only one specific directory and not to other folders. For example, python script creates folder1, folder 2, ...I want folder1 to be readable by user 1 only, and folder2 only by user2 ...etc. Working on Windows Server 2008. Thank you.

Z77
  • 1,097
  • 6
  • 19
  • 30

2 Answers2

0

os.chown(path, uid, gid) to change the path owner, and then os.chmod(path, mode) with mode 700 (Only you can read, write to, or execute the path). I think that should work..

---EDIT---

apparantly chown dosent work for windows. looks like you will need the win32security module, which is a part of pywin32. here is an answer demonstrating how to change permissions with it.

Community
  • 1
  • 1
WeaselFox
  • 7,220
  • 8
  • 44
  • 75
0

The Win32 security APIs are not for the faint hearted. The simplest solution is to use an external Microsoft program like cacls or icacls called from Python, using subprocess or system. cacls is rather messy:

cmd = r"echo y|cacls gash.txt /E /G BUILTIN\\Users:R"

cacls is documented in the online help (Windows XP) and here. It may output a prompt (depending on the action) "Are you sure (Y/N)?", which is why we have the echo y| in the example (see here). Alternatively use xcacls.exe from the Windows Resource Kit - this version does not prompt.

At Windows Vista and Windows 7 the cacls program still works but is deprecated in favour of the more powerful icacls program, which thankfully does not prompt. Sorry, I don't have Windows 8.

cdarke
  • 42,728
  • 8
  • 80
  • 84