How to remove read-only attrib directory with Python in Windows?

前端 未结 5 592
野的像风
野的像风 2020-11-29 08:20

I have a read only directory copied from version controlled directory which is locked. \"enter

相关标签:
5条回答
  • 2020-11-29 08:35

    The accepted answer is almost right, but it could fail in case of a read-only subdirectory.

    The function is given as an argument to the rmtree's onerror handler.

    I would suggest:

    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)
    

    In case the function fails again, you can see the reason, and continue deleting.

    0 讨论(0)
  • 2020-11-29 08:37

    If you are using shutil.rmtree, you can use the onerror member of that function to provide a function that takes three params: function, path, and exception info. You can use this method to mark read only files as writable while you are deleting your tree.

    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 )
    

    Now, to be fair, the error function could be called for a variety of reasons. The 'func' parameter can tell you what function "failed" (os.rmdir() or os.remove()). What you do here depends on how bullet proof you want your rmtree to be. If it's really just a case of needing to mark files as writable, you could do what I did above. If you want to be more careful (i.e. determining if the directory coudln't be removed, or if there was a sharing violation on the file while trying to delete it), the appropriate logic would have to be inserted into the on_rm_error() function.

    0 讨论(0)
  • 2020-11-29 08:39
    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)
    

    copy from:http://code.activestate.com/recipes/303343-changing-file-attributes-on-windows/

    0 讨论(0)
  • 2020-11-29 08:41

    The method that I have used is to do:

    if os.path.exists(target) :
        subprocess.check_call(('attrib -R ' + target + '\\* /S').split())
        shutil.rmtree(target)
    

    Before anyone jumps on me, I know that this is dreadfully un-pythonic, but it is possibly simpler than the more traditional answers given above, and has been reliable.

    I'm not sure what happens regarding read/write attributes on directories. But it hasn't been an issue yet.

    0 讨论(0)
  • 2020-11-29 08:46

    Not tested but It would be, something like to enable write access.

    import os, stat
    
    os.chmod(ur"file_path_name", stat.S_IWRITE)
    

    You may need to combine with os.walk to make everything write enable. something like

    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)
    
    0 讨论(0)
提交回复
热议问题