使用Python查找并删除目录中的特定文件和子目录 [英] Find and delete specific file and sub directory within a directory using Python
问题描述
我正在尝试自动执行特定文件夹下特定文件和文件夹的搜索和删除操作。下面是我有的文件夹结构:
主目录是MasterFolder,它包括多个子目录,子目录Fol1,Fol2,Fol3,Fol4子目录可能不同文件夹到文件夹。
子文件夹包含更多的文件和子文件夹。 ExL Fol1拥有一些文件夹,sometext.txt,AnotherFilesFolder同样适用于MasterFolder下的其他Fol2,Fol3等子目录。
现在我想做的是伤口扫描MasterFolder并浏览每个ChildFolder,并在每个子文件夹下查找1个名为someText.txt的文件和1个名为someFilesFolder的文件夹,并删除它们。理想情况下,我想要删除的文件夹名称和文件名在每个ChildFolder下都是相同的,所以查找应该只发生在MasterFolder的一个级别。我检查了多篇文章,但一切都指定使用shutil.rmtree在一个文件夹下删除特定的文件或目录,但我正在寻找的东西,将执行查找和删除递归我相信。
<理想情况下,我想要删除的文件夹名称和文件名在每个ChildFolder下都是一样的,所以这个查找应该只发生在MasterFolder的一个级别。
通过 MasterFolder
中的每个子文件夹的一个简单方法是遍历[ os.listdir]('/ path / to / MasterFolder')
。这会给你两个文件和子文件夹。您可以使用 os .path.isdir
。但是,对 try
操作它们就好像它们都是文件夹一样简单(而且更高效,更清洁),并且通过不做任何事情来处理非文件夹的异常/日志记录/什么似乎适当。
您从 listdir
返回的列表只是裸露的名称,所以您将需要 os.path.join
将每个名称连接到 / path / to / MasterFolder
。当然,你也需要使用它连接someTxt.txt
和someFilesFolder
。
最后,在每个子目录中,只要 listdir
删除文件和子目录(如果它们存在的话),再简单一点(更清洁,更高效),只要 try
。你显然已经知道如何 shutil.rmtree
和 os.unlink
,所以...你完成了。
如果理想情况下没有实际保证,而不是 os.listdir
,你将不得不使用 os.walk
。这稍微复杂一点,但是如果你看看例子,那么回过头来阅读例子中的细节文档,不难搞清楚。
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:
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.
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.
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.
To get you started:
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.
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.
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.
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.
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查找并删除目录中的特定文件和子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!