python chmod windows 权限_Chmod问题使用python更改文件权限

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

解决方案

I found a solution here :)

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值