Python目录通过dict搜索和组织 [英] Python directory searching and organizing by dict
问题描述
所以例如一棵树可能看起来像这样
- 起始路径
- 目录1
- Subdir 1
- Subdir 2
- Subdir 3
- subsubdir
- file.jpg
- folder1
- file1.jpg
- file2.jpg
- folder2
- file3.jpg
- file4.jpg
- subsubdir
- 目录1
即使subsubdir有一个文件,它应该被跳过,因为它有文件夹。
现在我通常可以这样做,如果我知道我要去多少目录要寻找,使用os.listdir和os.path.isdir。但是,如果我想要这样做是动态的,它将不得不补偿任何数量的文件夹和子文件夹。我一直在使用os.walk尝试,它会很容易找到的所有文件。我遇到的唯一麻烦是创造所有包含文件的路径名的类型的字典。我需要由dict组织的文件夹名称,直到起始路径。
所以最后,使用上面的例子,dict应该是这样的,其中包含文件:
dict ['dir1'] ['subdir3'] ['subsubdir'] ['folder1'] = ['file1.jpg','file2.jpg']
dict ['dir1'] ['subdir3'] ['subsubdir'] ['folder2'] = ['file3.jpg','file4.jpg']
感谢任何关于组织这些信息的更好的想法的帮助。谢谢。
也许你想要的东西:
def explore(starting_path):
alld = {'':{}}
for dirpath,dirnames,os.walk中的文件名(starting_path):
d = alld
dirpath = dirpath [len(starting_path):]
dirpath.split(os.sep)中的子目录:
based = d
d = d [subd]
如果dirnames:
for dn in dirnames:
d [dn] = {}
else:
based [subd] = filenames
return alld [' ']
例如,给定一个 / tmp / a
,以便:
$ ls -FR / tmp / a
b / c / d /
/ tmp / a / b:
z /
/ tmp / a / b / z:
/ tmp / a / c:
za zu
/ tmp / a / d:
code> print explore('/ tmp / a')发出: {'c':['za','zu'],'b' 'z':[]},'d':[]}
。
如果这是牛逼正是你以后,也许你可以明确告诉我们什么差别应该是什么?我怀疑他们也许可以很容易地固定,如果需要的话。
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.
Maybe you want something like:
def explore(starting_path):
alld = {'': {}}
for dirpath, dirnames, filenames in os.walk(starting_path):
d = alld
dirpath = dirpath[len(starting_path):]
for subd in dirpath.split(os.sep):
based = d
d = d[subd]
if dirnames:
for dn in dirnames:
d[dn] = {}
else:
based[subd] = filenames
return alld['']
For example, given a /tmp/a
such that:
$ ls -FR /tmp/a
b/ c/ d/
/tmp/a/b:
z/
/tmp/a/b/z:
/tmp/a/c:
za zu
/tmp/a/d:
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.
这篇关于Python目录通过dict搜索和组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!