使用 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.rmtree
和 os.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.
相关文章