python - 使用 python 更改文件权限的 Chmod 问题

标签 python windows python-2.7 chmod

我正在寻找更改所有文件的文件权限,以便使用 python 脚本为目录中的所有用户读写和执行。但是,当我通过右键单击检查文件权限时运行脚本后,它只显示我的权限和组中的每个人它只有读取权限。我在以下脚本中做错了什么:

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

最佳答案

我在这里找到了解决方案:)

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)

关于python - 使用 python 更改文件权限的 Chmod 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27500067/

相关文章:

windows - Windows API 调用的内存泄漏问题 - Delphi

python-2.7 - 根据列中的 True 值过滤行 - python pandas dataframe

python - 当我将参数通过管道传递给 Django shell 时如何将参数传递给 python 脚本

python - 模块未找到错误 : No module named 'xlrd'

C# ML.Net 图像分类 : Does GPU acceleration help improve the performance of predictions and how can I tell if it is?

windows - 通过rsync或cp将文件发送到运行micropython的ESP32时,Windows上的Rshell随机卡住

python - 将 CSV 数据流转换为 Pandas DataFrame (Python 2.7)

python - 在 Python 2.7 中创建套接字时出错

python - BioPython AlignIO ValueError 说字符串必须具有相同的长度?

python - 优化代码以找到可被 1-20 之间的所有整数整除的最小数字