Python目录通过dict搜索和组织 [英] Python directory searching and organizing by dict

查看:144
本文介绍了Python目录通过dict搜索和组织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,这是我第一次最近试图进入Python的文件和os部分。我正在尝试搜索一个目录,然后找到所有子目录。如果目录没有文件夹,请将所有文件添加到列表中。并且通过dict来组织它们。



所以例如一棵树可能看起来像这样




  • 起始路径


    • 目录1


      • Subdir 1

      • Subdir 2

      • Subdir 3


        • subsubdir


          • file.jpg

          • folder1


            • file1.jpg

            • file2.jpg


          • folder2


            • file3.jpg

            • file4.jpg








即使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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆