Chmod issue to change file permission with python

I want to change the file permission for all read and write files for all users in a directory using a python script. However, after running the script, when I check the permissions on the file by right clicking, it only shows the permissions for me, and for the group, everyone who 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


source to share


2 answers


I found a solution here :)

Setting folder permissions on 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)

      

+6


source


As per the os.chmod

documentation
note :



Although Windows supports chmod (), you can only set the file to read-only (via stat.S_IWRITE

both stat.S_IREAD

constants or the corresponding integer value). All other bits are ignored.

+4


source







All Articles