Permission Change Of Files In Python
I want to change the file permission for all the files from my current directory tree. I am trying to open each directory and open the files and change the permission using os.chmo
Solution 1:
You are using os.walk
incorrectly.
for dirpath, dirnames, filenames inos.walk('.'):
for filename in filenames:
path = os.path.join(dirpath, filename)
os.chmod(path, 0o777) # for example
Solution 2:
You can instead use an OS specific function call as follows:
os.system('chmod 777 -R *')
Solution 3:
If you just want to make the file open to everyone, you can use the following class:
import win32security
import ntsecuritycon
classWin32FileSecurityMod:
def__init__(self):
self.security_dict = {}
# return tuple from LookupAccountName() is user, domain, type
self.security_dict["Everyone"] = win32security.LookupAccountName("", "Everyone")
self.security_dict["Administrators"] = win32security.LookupAccountName("", "Administrators")
self.security_dict["CurrentUser"] = win32security.LookupAccountName("", win32api.GetUserName())
self.admins = self.security_dict["Administrators"][0]
self.everyone = self.security_dict["Everyone"][0]
self.user = self.security_dict["CurrentUser"][0]
defSetPromiscuousFileAccess(self, file_path):
print(file_path)
con = ntsecuritycon
sd = win32security.GetFileSecurity(file_path,win32security.DACL_SECURITY_INFORMATION)
dacl = win32security.ACL()
# con.FILE_GENERIC_READ# con.FILE_GENERIC_READ | con.FILE_GENERIC_WRITE,# con.FILE_ALL_ACCESS
dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_ALL_ACCESS, self.everyone)
dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_ALL_ACCESS, self.user)
dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_ALL_ACCESS, self.admins)
# Put our new DACL into the Security Descriptor,# update the file with the updated SD, and use# CACLS to show what's what.
sd.SetSecurityDescriptorDacl(1, dacl, 0)
win32security.SetFileSecurity(file_path, win32security.DACL_SECURITY_INFORMATION, sd)
# Gets the attributes of the file by executing a shell command. Useful for testing# but it will have performance problems for large file sets.defGetCacls(self, file_path):
out = []
for line in os.popen("cacls %s" % file_path).read().splitlines():
out.append(line)
return out
Post a Comment for "Permission Change Of Files In Python"