Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Windows中使用Python删除只读attrib目录?_Python_Windows_Attributes - Fatal编程技术网

如何在Windows中使用Python删除只读attrib目录?

如何在Windows中使用Python删除只读attrib目录?,python,windows,attributes,Python,Windows,Attributes,我有一个从版本控制目录复制的只读目录,该目录已锁定。 当我试图用shutil.rmtree(TEST\u OBJECTS\u DIR)命令删除这个目录时,我收到了以下错误消息 WindowsError: [Error 5] Access is denied: 'C:\...\environment.txt' 问:如何更改整个目录结构中所有内容的属性 未测试,但可能是,类似于启用写访问 import os, stat os.chmod(ur"file_path_name", stat.S_

我有一个从版本控制目录复制的只读目录,该目录已锁定。

当我试图用
shutil.rmtree(TEST\u OBJECTS\u DIR)
命令删除这个目录时,我收到了以下错误消息

WindowsError: [Error 5] Access is denied: 'C:\...\environment.txt'
  • 问:如何更改整个目录结构中所有内容的属性

未测试,但可能是,类似于启用写访问

import os, stat

os.chmod(ur"file_path_name", stat.S_IWRITE)
您可能需要与os.walk结合使用,以启用所有写操作。差不多

for root, dirs, files in os.walk(ur'root_dir'):
    for fname in files:
        full_path = os.path.join(root, fname)
        os.chmod(full_path ,stat.S_IWRITE)

如果您使用的是shutil.rmtree,则可以使用该函数的onerror成员提供一个具有三个参数的函数:函数、路径和异常信息。删除树时,可以使用此方法将只读文件标记为可写文件

import os, shutil, stat

def on_rm_error( func, path, exc_info):
    # path contains the path of the file that couldn't be removed
    # let's just assume that it's read-only and unlink it.
    os.chmod( path, stat.S_IWRITE )
    os.unlink( path )

shutil.rmtree( TEST_OBJECTS_DIR, onerror = on_rm_error )

现在,为了公平起见,可以出于各种原因调用error函数。“func”参数可以告诉您什么函数“失败”(os.rmdir()或os.remove())。你在这里做什么取决于你希望你的rmtree有多防弹。如果这真的只是需要将文件标记为可写的,那么您可以像我上面所做的那样。如果您想更加小心(即确定目录是否无法删除,或者在尝试删除文件时文件是否存在共享冲突),则必须将适当的逻辑插入on_rm_error()函数。

我使用的方法是:

if os.path.exists(target) :
    subprocess.check_call(('attrib -R ' + target + '\\* /S').split())
    shutil.rmtree(target)
在任何人攻击我之前,我知道这是非常不符合python的,但它可能比上面给出的更传统的答案更简单,并且是可靠的

我不确定目录上的读/写属性会发生什么情况。但这还不是个问题

import win32con, win32api,os

file='test.txt'

#make the file hidden
win32api.SetFileAttributes(file,win32con.FILE_ATTRIBUTE_HIDDEN)

#make the file read only
win32api.SetFileAttributes(file,win32con.FILE_ATTRIBUTE_READONLY)

#to force deletion of a file set it to normal
win32api.SetFileAttributes(file, win32con.FILE_ATTRIBUTE_NORMAL)
os.remove(file)

复制自:

接受的答案几乎正确,但如果是只读子目录,则可能会失败

该函数作为
rmtree
onerror
处理程序的参数提供

我建议:

import os, shutil, stat

def remove_readonly(fn, path, excinfo):
    try:
        os.chmod(path, stat.S_IWRITE)
        fn(path)
    except Exception as exc:
        print "Skipped:", path, "because:\n", exc

shutil.rmtree(TEST_OBJECTS_DIR, onerror=remove_readonly)

如果该功能再次失败,您可以查看原因,然后继续删除。

此处的更多详细信息:酷!这是一种更为专用的方法。对我来说,这是使用shutil.rmtree安全删除.git文件夹(包括只读文件)和.git文件的唯一有效方法!这个解决方案可以在windows上运行(只要attrib.exe存在),并且不需要外部python模块。这个解决方案非常好,我发现它更棒efficient@Nils到目前为止,这里提供的其他解决方案都不需要任何外部Python模块。编辑:好的,我错过了win32con的内容。请注意,您可以将导入的数量减少到只
win32file
。因此:
win32file.SetFileAttributes(文件,win32file.file\u属性\u只读)