• <bdo id='wbzFw'></bdo><ul id='wbzFw'></ul>
    <tfoot id='wbzFw'></tfoot>
      <i id='wbzFw'><tr id='wbzFw'><dt id='wbzFw'><q id='wbzFw'><span id='wbzFw'><b id='wbzFw'><form id='wbzFw'><ins id='wbzFw'></ins><ul id='wbzFw'></ul><sub id='wbzFw'></sub></form><legend id='wbzFw'></legend><bdo id='wbzFw'><pre id='wbzFw'><center id='wbzFw'></center></pre></bdo></b><th id='wbzFw'></th></span></q></dt></tr></i><div id='wbzFw'><tfoot id='wbzFw'></tfoot><dl id='wbzFw'><fieldset id='wbzFw'></fieldset></dl></div>

        <legend id='wbzFw'><style id='wbzFw'><dir id='wbzFw'><q id='wbzFw'></q></dir></style></legend>

        <small id='wbzFw'></small><noframes id='wbzFw'>

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

        时间:2023-09-13

            <bdo id='0Dz0K'></bdo><ul id='0Dz0K'></ul>

            • <tfoot id='0Dz0K'></tfoot>

                1. <i id='0Dz0K'><tr id='0Dz0K'><dt id='0Dz0K'><q id='0Dz0K'><span id='0Dz0K'><b id='0Dz0K'><form id='0Dz0K'><ins id='0Dz0K'></ins><ul id='0Dz0K'></ul><sub id='0Dz0K'></sub></form><legend id='0Dz0K'></legend><bdo id='0Dz0K'><pre id='0Dz0K'><center id='0Dz0K'></center></pre></bdo></b><th id='0Dz0K'></th></span></q></dt></tr></i><div id='0Dz0K'><tfoot id='0Dz0K'></tfoot><dl id='0Dz0K'><fieldset id='0Dz0K'></fieldset></dl></div>
                  <legend id='0Dz0K'><style id='0Dz0K'><dir id='0Dz0K'><q id='0Dz0K'></q></dir></style></legend>
                    <tbody id='0Dz0K'></tbody>

                  <small id='0Dz0K'></small><noframes id='0Dz0K'>

                  本文介绍了如何在 Windows 中使用 Python 删除只读属性目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

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

                  当我尝试使用 shutil.rmtree(TEST_OBJECTS_DIR) 命令删除此目录时,我收到以下错误消息.

                  When I tried to remove this directory with shutil.rmtree(TEST_OBJECTS_DIR) command, I got the following error message.

                  WindowsError: [Error 5] Access is denied: 'C:...environment.txt'
                  

                  • 问:如何更改整个目录结构中所有内容的属性?
                    • Q : How can I change the attribute of everything in a whole directory structure?
                    • 推荐答案

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

                      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 )
                      

                      现在,公平地说,可以出于多种原因调用错误函数.'func' 参数可以告诉您哪个函数失败"(os.rmdir() 或 os.remove()).你在这里做什么取决于你希望你的 rmtree 有多防弹.如果它真的只是需要将文件标记为可写的情况,你可以做我上面做的事情.如果您想更加小心(即确定是否无法删除目录,或者在尝试删除文件时是否存在共享冲突),则必须将适当的逻辑插入 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.

                      这篇关于如何在 Windows 中使用 Python 删除只读属性目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在python中从字典中设置属性 下一篇:在 Python 中进行自动属性分配的最佳方法是什么,

                  相关文章

                  最新文章

                2. <i id='PvdxB'><tr id='PvdxB'><dt id='PvdxB'><q id='PvdxB'><span id='PvdxB'><b id='PvdxB'><form id='PvdxB'><ins id='PvdxB'></ins><ul id='PvdxB'></ul><sub id='PvdxB'></sub></form><legend id='PvdxB'></legend><bdo id='PvdxB'><pre id='PvdxB'><center id='PvdxB'></center></pre></bdo></b><th id='PvdxB'></th></span></q></dt></tr></i><div id='PvdxB'><tfoot id='PvdxB'></tfoot><dl id='PvdxB'><fieldset id='PvdxB'></fieldset></dl></div>
                    <bdo id='PvdxB'></bdo><ul id='PvdxB'></ul>

                    <small id='PvdxB'></small><noframes id='PvdxB'>

                    <legend id='PvdxB'><style id='PvdxB'><dir id='PvdxB'><q id='PvdxB'></q></dir></style></legend>
                    1. <tfoot id='PvdxB'></tfoot>