7

I am looking to change the file permission to all files to read write and execute for all the users in a directory using a python script. However, after running the script when I check the file permission doing the right click, it only shows the permissions for me and for the group everyone it only has read permission. Is there anything wrong I am doing in the following script:

import os
import pdb

for dirpath, dirnames, filenames in os.walk('M:\intra\EU'):
    for filename in filenames:
        path = os.path.join(dirpath, filename)
        os.chmod(path, 0o777) # for example

3 Answers 3

13

I found a solution here :)

Setting folder permissions in Windows using Python

import win32security
import ntsecuritycon as con
import os
import pdb
userx, domain, type = win32security.LookupAccountName ("", "Everyone")
directory='M:\intra\EU'
for dirpath, dirnames, filenames in os.walk('M:\intra\EU'):
    for FILENAME in filenames:
        sd = win32security.GetFileSecurity(directory+'\\'+FILENAME, win32security.DACL_SECURITY_INFORMATION)
        dacl = sd.GetSecurityDescriptorDacl()   # instead of dacl = win32security.ACL()
        dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_ALL_ACCESS, userx)
        sd.SetSecurityDescriptorDacl(1, dacl, 0)
        win32security.SetFileSecurity(directory+'\\'+FILENAME, win32security.DACL_SECURITY_INFORMATION, sd)
Sign up to request clarification or add additional context in comments.

2 Comments

Is the directory we are allowing access to; M:\intra\EU'? Do I have to put my credentials in LookupAccountName?
Those modules seem to be unavailable in Python3. I posted a different approach here as another answer.
11

According to the NOTE of the os.chmod documentation:

Although Windows supports chmod(), you can only set the file's read-only flag with it (via the stat.S_IWRITE and stat.S_IREAD constants or a corresponding integer value). All other bits are ignored.

1 Comment

Thanks falsetru, Is there any other solution to that, how can i set permission so that other users can edit the files I create using some python script. My python scripts create some files in a shared directory, that I want other people to be able to edit as well. i cant do it manually as these are a lot of files. Any ideas how I can achieve that in windows
7

The recommended solution didn't work on Python3 (modules not available). I took a different approach, to use the Windows command line.

In my case, I needed the "LOCAL SERVICE" account to have permission. I did:

    import subprocess
    args = ["icacls", directory,
            "/grant:r", 'LOCAL SERVICE:(OI)(CI)MF']
    subprocess.check_call(args)

Note that this permission seems to only work when set on a directory. For security reasons, it would also be a good idea to ensure that "directory" actually exists.

Also note that "LOCAL SERVICE" might go by a translated name. In German locale, for example, it is "Lokaler Dienst."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.