<small id='2WOW7'></small><noframes id='2WOW7'>

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

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

      使用 Python 查找和删除目录中的特定文件和子目录

      Find and delete specific file and sub directory within a directory using Python(使用 Python 查找和删除目录中的特定文件和子目录)

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

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

          • <bdo id='FQAga'></bdo><ul id='FQAga'></ul>
            <tfoot id='FQAga'></tfoot>
            1. <legend id='FQAga'><style id='FQAga'><dir id='FQAga'><q id='FQAga'></q></dir></style></legend>

                <tbody id='FQAga'></tbody>

              • 本文介绍了使用 Python 查找和删除目录中的特定文件和子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试对特定文件夹下的特定文件和文件夹自动执行搜索和删除操作.以下是我拥有的文件夹结构:

                I am trying to automate a search and delete operation for specific files and folder underneath a specific folder. Below is the folder structure which I have:

                主目录是 MasterFolder,其中包括多个子目录,即子文件夹 Fol1、Fol2、Fol3、Fol4,子目录可能因文件夹而异.

                Primary Directory is MasterFolder, which includes multiple sub directories which are Child Folders Fol1, Fol2, Fol3, Fol4 the sub directories may vary folder to folder.

                子文件夹有更多的文件和子文件夹.ExL Fol1 包含 someFilesFolder、sometext.txt、AnotherFilesFolder,同样适用于 MasterFolder 下的其他 Fol2、Fol3 等子目录.

                The Sub folders have more files and subfolders. ExL Fol1 holds someFilesFolder, sometext.txt, AnotherFilesFolder same applies to other Fol2,Fol3 etc sub directories under the MasterFolder.

                现在我想做的是我想扫描 MasterFolder 并遍历每个 ChildFolder 并在每个子文件夹下查找 1 个名为 someText.txt 的文件和 1 个名为 someFilesFolder 的文件夹,然后将其删除.理想情况下,我要删除的文件夹名和文件名在每个 ChildFolder 下都是相同的,因此查找应该只发生在 MasterFolder 的下一级.我检查了多篇文章,但所有内容都指定使用 shutil.rmtree 在一个文件夹下删除特定文件或目录,但我正在寻找可以递归查找和删除的东西,我相信.

                Now what I would like to do is I wound want to scan the MasterFolder and go through every ChildFolder and look for 1 file named someText.txt and 1 folder named someFilesFolder under every child folder and remove the same. Ideally the folder name and file name I would want to delete is same under every ChildFolder, so the find should happen only one level down the MasterFolder. I checked multiple articles but everything specifies deleting a specific file or a directory using shutil.rmtree under one folder, but I am looking for something which will do the find and delete recursively I believe.

                推荐答案

                让你开始:

                理想情况下,我要删除的文件夹名和文件名在每个 ChildFolder 下都是相同的,因此查找应该只发生在 MasterFolder 的下一级.

                Ideally the folder name and file name I would want to delete is same under every ChildFolder, so the find should happen only one level down the MasterFolder.

                遍历 MasterFolder 下的每个子文件夹的一种简单方法是遍历 [os.listdir]('/path/to/MasterFolder').这将为您提供文件和子文件夹.您可以使用 os.path 检查它们.是目录.但是,只需 try 就好像它们都是文件夹一样对它们进行操作,并通过什么都不做/记录/任何看起来合适的方式来处理非文件夹上的异常,这要简单得多(并且更有效、更干净).

                One easy way to go through every child folder under MasterFolder is to loop over [os.listdir]('/path/to/MasterFolder'). This will give you both files and child folders. You can check them each with os.path.isdir. But it's much simpler (and more efficient, and cleaner) to just try to operate on them as if they were all folders, and handle the exceptions on non-folders by doing nothing/logging/whatever seems appropriate.

                你从 listdir 得到的列表只是简单的名字,所以你需要 os.path.join 将每个名称连接到 /path/to/MasterFolder.当然,您还需要使用它来连接 "someTxt.txt""someFilesFolder".

                The list you get back from listdir is just bare names, so you will need os.path.join to concatenate each name to /path/to/MasterFolder. And you'll need to use it to concatenate "someTxt.txt" and "someFilesFolder" as well, of course.

                最后,虽然您可以 listdir 再次在每个子目录上,并且仅删除存在的文件和子目录,但同样,它更简单(更简洁,更高效) 到只是 try 每一个.你显然已经知道如何 shutil.rmtreeos.unlink,所以……你已经完成了.

                Finally, while you could listdir again on each child directory, and only delete the file and subdirectory if they exist, again, it's simpler (and cleaner and more efficient) to just try each one. You apparently already know how to shutil.rmtree and os.unlink, so…you're done.

                如果实际上不能保证理想",那么您将不得不使用 os.listdir/os.html#os.walk" rel="nofollow">os.walk.这稍微复杂一些,但是如果您查看示例,然后返回并阅读示例上方的文档以了解详细信息,就不难理解了.

                If that "ideally" isn't actually guaranteed, instead of os.listdir, you will have to use os.walk. This is slightly more complicated, but if you look at the examples, then come back up and read the docs above the examples for the details, it's not hard to figure out.

                这篇关于使用 Python 查找和删除目录中的特定文件和子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                <i id='uUjrW'><tr id='uUjrW'><dt id='uUjrW'><q id='uUjrW'><span id='uUjrW'><b id='uUjrW'><form id='uUjrW'><ins id='uUjrW'></ins><ul id='uUjrW'></ul><sub id='uUjrW'></sub></form><legend id='uUjrW'></legend><bdo id='uUjrW'><pre id='uUjrW'><center id='uUjrW'></center></pre></bdo></b><th id='uUjrW'></th></span></q></dt></tr></i><div id='uUjrW'><tfoot id='uUjrW'></tfoot><dl id='uUjrW'><fieldset id='uUjrW'></fieldset></dl></div>
                    <tbody id='uUjrW'></tbody>

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

                        • <bdo id='uUjrW'></bdo><ul id='uUjrW'></ul>
                          <tfoot id='uUjrW'></tfoot>

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