【问题标题】:permission change of files in pythonpython中文件的权限更改
【发布时间】:2011-11-05 21:31:57
【问题描述】:

我想更改当前目录树中所有文件的文件权限。我正在尝试打开每个目录并打开文件并使用os.chmod() 更改权限,但出现错误。

import os
import stat

for files in os.walk('.'):
        os.chmod(files,stat.S_IXGRP)

我得到的错误是:

File "delhis.py", line 4, in ? os.chmod(files,stat.S_IXGRP) TypeError: coercing to Unicode: need string or buffer, tuple found

【问题讨论】:

  • 请告诉我们你得到了什么。
  • @All 这是我得到的错误:文件“delhis.py”,第 4 行,在? os.chmod(files,stat.S_IXGRP) TypeError: coercing to Unicode: need string or buffer, tuple found

标签: python


【解决方案1】:

您错误地使用了os.walk

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

【讨论】:

  • 为了证明,而不是运行 chmod,将您当前的代码更改为 print files
【解决方案2】:

您可以改为使用特定于操作系统的函数调用,如下所示:

os.system('chmod 777 -R *')

【讨论】:

    【解决方案3】:

    如果你只是想让文件对所有人开放,你可以使用下面的类:

    import win32security
    import ntsecuritycon
    
    class Win32FileSecurityMod:
      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]
    
      def SetPromiscuousFileAccess(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.
      def GetCacls(self, file_path):
        out = []
        for line in os.popen("cacls %s" % file_path).read().splitlines():
          out.append(line)
        return out
    

    【讨论】:

      猜你喜欢
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 2013-06-27
      • 2012-04-27
      • 2020-08-12
      • 2016-01-20
      相关资源
      最近更新 更多