Python 通过 dict 进行目录搜索和组织
大家好,这是我最近第一次尝试进入Python的文件和操作系统部分。我正在尝试搜索一个目录然后找到所有子目录。如果该目录没有文件夹,则将所有文件添加到列表中。并通过听写来组织它们。
例如,一棵树可能看起来像这样的
- 起始路径
- 目录 1
- 子目录 1
- 子目录 2
- 子目录 3
- 子子目录
- 文件.jpg
- 文件夹1
- 文件1.jpg
- 文件2.jpg
- 文件夹2
- 文件3.jpg
- 文件4.jpg
- 子子目录
- 目录 1
即使 subsubdir 中有文件,也应该跳过它,因为它有文件夹。
现在,如果我知道要查找多少个目录,则通常可以使用 os.listdir 和 os.path.isdir 来执行此操作。但是,如果我希望它是动态的,则必须补偿任意数量的文件夹和子文件夹。我尝试过使用 os.walk,它会轻松找到所有文件。我遇到的唯一麻烦是使用包含文件的路径名创建所有字典。我需要按 dict 组织的文件夹名称,直到起始路径。
因此,最后,使用上面的示例,字典及其中的文件应如下所示:
dict['dir1']['subdir3']['subsubdir']['folder1'] = ['file1.jpg', 'file2.jpg']
dict['dir1']['subdir3']['subsubdir']['folder2'] = ['file3.jpg', 'file4.jpg']
希望对此提供任何帮助或组织信息的更好想法。谢谢。
Hey all, this is my first time recently trying to get into the file and os part of Python. I am trying to search a directory then find all sub directories. If the directory has no folders, add all the files to a list. And organize them all by dict.
So for instance a tree could look like this
- Starting Path
- Dir 1
- Subdir 1
- Subdir 2
- Subdir 3
- subsubdir
- file.jpg
- folder1
- file1.jpg
- file2.jpg
- folder2
- file3.jpg
- file4.jpg
- subsubdir
- Dir 1
Even if subsubdir has a file in it, it should be skipped because it has folders in it.
Now I can normally do this if I know how many directories I am going to be looking for, using os.listdir and os.path.isdir. However if I want this to be dynamic it will have to compensate for any amount of folders and subfolders. I have tried using os.walk and it will find all the files easily. The only trouble I am having is creating all the dicts with the path names that contain file. I need the foldernames organized by dict, up until the starting path.
So in the end, using the example above, the dict should look like this with the files in it:
dict['dir1']['subdir3']['subsubdir']['folder1'] = ['file1.jpg', 'file2.jpg']
dict['dir1']['subdir3']['subsubdir']['folder2'] = ['file3.jpg', 'file4.jpg']
Would appreciate any help on this or better ideas on organizing the information. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许你想要类似的东西:
例如,给定一个
/tmp/a
这样:print explore('/tmp/a')
发出:{'c ':['za','zu'],'b':{'z':[]},'d':[]}
。如果这不正是您所追求的,也许您可以具体向我们展示其中的差异是什么?我怀疑如果需要的话,它们可能很容易修复。
Maybe you want something like:
For example, given a
/tmp/a
such that:print explore('/tmp/a')
emits:{'c': ['za', 'zu'], 'b': {'z': []}, 'd': []}
.If this isn't exactly what you're after, maybe you can show us specifically what the differences are supposed to be? I suspect they can probably be easily fixed, if need be.
我不知道你为什么要这样做。您应该能够使用 os.path.walk 进行处理,但如果您确实需要这样的结构,您可以这样做(未经测试):
此外,您不应该将变量命名为
dict
因为它是 Python 内置的。将名称dict
重新绑定到 Python 的dict
类型以外的名称是一个非常糟糕的主意。编辑:编辑以修复“双最后一个键”错误并使用
os.walk
。I don't know why you would want to do this. You should be able to do your processing using
os.path.walk
, but in case you really need such a structure, you can do (untested):Also, you should not name your variable
dict
because it's a Python built-in. It is a very bad idea to rebind the namedict
to something other than Python'sdict
type.Edit: edited to fix the "double last key" error and to use
os.walk
.您想要构建数据的方式存在一个基本问题。如果 dir1/subdir1 包含子目录和文件,那么 dict['dir1']['subdir1'] 应该是列表还是字典?要使用
...['subdir2']
访问更多子目录,它需要是一个字典,但另一方面dict['dir1']['subdir1']
应该返回文件列表。您要么必须从以某种方式结合这两个方面的自定义对象构建树,要么必须更改树结构以以不同的方式处理文件。
There is a basic problem with the way you want to structure the data. If
dir1/subdir1
contains subdirectories and files, shoulddict['dir1']['subdir1']
be a list or a dictionary? To access further subdirectories with...['subdir2']
it needs to be a dictionary, but on the other handdict['dir1']['subdir1']
should return a list of files.Either you have to build the tree from custom objects that combine these two aspects in some way, or you have to change the tree structure to treat files differently.