如何使用Python遍历目录?

39 投票
4 回答
75770 浏览
提问于 2025-04-15 23:15

我有一个叫做“notes”的文件夹,里面会按照类别再分成多个文件夹,而这些文件夹里还会有子文件夹,用来放更细的分类。现在我遇到的问题是,我有一个函数可以遍历三层的子目录:

def obtainFiles(path):
      list_of_files = {}
      for element in os.listdir(path):
          # if the element is an html file then..
          if element[-5:] == ".html":
              list_of_files[element] = path + "/" + element
          else: # element is a folder therefore a category
              category = os.path.join(path, element)
              # go through the category dir
              for element_2 in os.listdir(category):
                  dir_level_2 = os.path.join(path,element + "/" + element_2)
                  if element_2[-5:] == ".html":
                      print "- found file: " + element_2
                      # add the file to the list of files
                      list_of_files[element_2] = dir_level_2
                  elif os.path.isdir(element_2):
                      subcategory = dir_level_2
                      # go through the subcategory dir
                      for element_3 in os.listdir(subcategory):
                          subcategory_path = subcategory + "/" + element_3
                        if subcategory_path[-5:] == ".html":
                            print "- found file: " + element_3
                            list_of_files[element_3] = subcategory_path
                        else:
                            for element_4 in os.listdir(subcategory_path):
                                 print "- found file:" + element_4

需要注意的是,这个功能还在不断完善中。在我看来,它的样子很糟糕……我想要实现的目标是,遍历所有的文件夹和子文件夹,把所有的文件名放到一个叫“list_of_files”的字典里,文件名作为“键”,而完整路径作为“值”。这个函数现在还不能正常工作,我想知道怎么使用os.walk这个函数来做类似的事情?

谢谢

4 个回答

3

我遇到过这个问题很多次,但没有一个答案让我满意,所以我创建了一个脚本来解决这个问题。在处理目录时,使用Python会显得非常麻烦。

下面是如何使用这个脚本的:

import file_walker


for f in file_walker.walk("/a/path"):
     print(f.name, f.full_path) # Name is without extension
     if f.isDirectory: # Check if object is directory
         for sub_f in f.walk(): # Easily walk on new levels
             if sub_f.isFile: # Check if object is file (= !isDirectory)
                 print(sub_f.extension) # Print file extension
                 with sub_f.open("r") as open_f: # Easily open file
                     print(open_f.read())
                
            
11

另一种选择是使用生成器,这个方法是基于@ig0774的代码。

import os
def walk_through_files(path, file_extension='.html'):
   for (dirpath, dirnames, filenames) in os.walk(path):
      for filename in filenames:
         if filename.endswith(file_extension): 
            yield os.path.join(dirpath, filename)

然后

for fname in walk_through_files():
    print(fname)
82

根据你提供的简短描述,像这样的代码应该可以正常工作:

list_of_files = {}
for (dirpath, dirnames, filenames) in os.walk(path):
    for filename in filenames:
        if filename.endswith('.html'): 
            list_of_files[filename] = os.sep.join([dirpath, filename])

撰写回答