Python中的子文件夹如何通过文件夹迭代
我有一个装有数千个.AI文件的文件夹。该文件夹的安排方式最初是带有客户名称的子文件夹,并且在每个子文件夹中,每个子文件夹是一个包含.AI的某些或多个子文件夹的唯一目录,或没有子文件夹只是.ai文件。
我需要一个程序,可以通过将每个.AI文件名在客户子文件夹内部(不管有多少个子文件夹或subs等人等)中迭代,并将其附加到列表中。然后,我将列出该列表,然后稍后再做一些OCR,但是一旦完成,我将清除列表并继续进入下一个子文件夹。
这是我试图尝试的代码,但失败了。它有时会返回一个空列表,或其中只有一个文件名的列表,当时它应该每次在其中带有一个或多个.AI文件名返回列表。
def folder_loop(folder):
temp_list = []
for root, dirs, files in os.walk(folder):
for dir in dirs:
for file in dir:
if file.endswith("ai"):
temp_list.append(os.path.join(root, file))
print(temp_list)
temp_list.clear()
我是一个野生动物,我几乎不明白代码在做什么,所以我并不感到惊讶,它无效。有什么想法吗?
I have a folder filled with thousands of .ai files. The way this folder is arranged is it initially has subfolders titled with a customer's name, and within each of those subfolders is a unique directory of some or many subfolders that contain .ai's, or subfolders within subfolders within subs... that contain .ai's, or no subfolders just .ai files.
I need a program that will iterate through this folder by taking every .ai filename inside of a customer subfolder(regardless of how many subfolders, or subs within subs, etc...) and append it to a list. Then I'll take that list and do some ocr stuff to it later, but once that's done I'll clear the list and move on to the next subfolder.
This is the code I used to attempt this but it failed. It returns an empty list sometimes, or a list with just one filename in it, when it should return a list each time with one or multiple .ai filenames inside of it.
def folder_loop(folder):
temp_list = []
for root, dirs, files in os.walk(folder):
for dir in dirs:
for file in dir:
if file.endswith("ai"):
temp_list.append(os.path.join(root, file))
print(temp_list)
temp_list.clear()
I'm a begginer and I hardly understand what the code is even doing so I'm not surprised it didn't work. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试以下操作:
如果您想将 base 文件夹给出所有客户文件夹的位置,然后为每个客户文件夹中的所有
列表都需要。
path.rglob(“*。ai”)
是递归 globbing 给定的路径
及其所有子文件夹的.ai
-files。要使用它:
如果您想给它一个文件夹,并且想要一个列表,其中包含所有
.ai
文件:此处的屈服/返回列表中包含
path> path
-Objects(非常方便)。如果您想要字符串,那么您可以这样做等。
You could try the following:
In case you want to give the function the base folder, in which all the customer folders are located, and then want for each of the customer folders a list of all
.ai
-files (from every sublevel):Path.rglob("*.ai")
is recursively globbing the the givenPath
with all its subfolders for.ai
-files.To use it:
If you want to give it a folder and want one list with all the
.ai
files in it:The yielded/returned lists here contain
Path
-objects (which are pretty handy). If you want strings instead, then you could doetc.
有一个社区帖子在这里答案。
话虽如此,我在个人实用程序工具箱中有以下方法。
There's a community post here which has some stupidly complete answers.
That being said, I have the method below in my personal utilities toolbox.