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.
2 Answers
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.
-
@BurhanKhalid - the docs state this about chomd: "Availability: Unix, Windows." - see http://docs.python.org/2/library/os.html#os.chmod – WeaselFox Feb 27 '14 at 08:11
-
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.

- 42,728
- 8
- 80
- 84