return Python 中字典的递归函数

Recursive function to return a dictionary in Python

我有上面的树。我需要以递归的方式搜索树中的目录和文件,并将它们 return 作为以下形式的字典 -> 键:文件的directories/name,值:文件的第一行

eg: key:1/2/5/test5    value:first line of test 5

到目前为止,我创建了下一个代码:

def search(root):
    items = os.listdir(root)
    
    for element in items:
        if os.path.isfile(element):
        
            with open (element) as file:
                one_line=file.readline()
                print(one_line)

        elif os.path.isdir(element):
            search(os.path.join(root,element))

问题是我的代码只搜索目录。请让我明白我错在哪里以及如何解决它。非常感谢任何帮助,谢谢!

您可以使用os.walk

以下功能将不包含空文件夹。

def get_tree(startpath):
    tree = {}
    for root, dirs, files in os.walk(startpath):
        for file in files:
            path = root+"/"+file
            with open(path,'r') as f:
                first_line =  f.readline()
            tree[path] = first_line
    return tree

输出将是这样的:

{
    file_path : first_line_of_the_file,
    file_path2 : first_line_of_the_file2,
    ...
}

您的代码几乎是正确的。不过,它必须稍微调整一下。 更具体地说,

  1. element 是文件或目录 name(不是路径)。如果它是子目录或子目录中的文件,if os.path.isfile(element)elif os.path.isdir(element) 的值将始终为 False。因此,将它们分别替换为 if os.path.isfile(os.path.join(root, element))elif os.path.isdir(os.path.join(root, element))

  2. 同理,with open(element)应该换成with open(os.path.join(root,element))

  3. 读取文件的第一行时,您必须将路径和该行存储在字典中。

  4. elif os.path.isdir(element)中调用递归函数时必须更新该字典。

请参阅下面的完整代码段:

import os

def search(root):

    my_dict = {}   # this is the final dictionary to be populated

    for element in os.listdir(root):
        
        if os.path.isfile(os.path.join(root, element)):
            try: 
                with open(os.path.join(root, element)) as file:
                    my_dict[os.path.join(root, element)] = file.readline() # populate the dictionary
            except UnicodeDecodeError: 
                # This exception handling has been put here to ignore decode errors (some files cannot be read)
                pass

        elif os.path.isdir(os.path.join(root, element)):
            my_dict.update(search(os.path.join(root,element)))  # update the current dictionary with the one resulting from the recursive call

    return my_dict

print(search('.'))

它打印如下字典:

{
 "path/file.csv": "name,surname,grade",
 "path/to/file1.txt": "this is the first line of file 1",
 "path/to/file2.py": "import os"
}

为了可读性,可以将os.path.join(root, element)存储在一个变量中,则:

import os

def search(root):

    my_dict = {}   # this is the final dictionary to be populated

    for element in os.listdir(root):
        path = os.path.join(root, element)

        if os.path.isfile(path):
            with open(path) as file:
                my_dict[path] = file.readline()

        elif os.path.isdir(path):
            my_dict.update(search(path))

    return my_dict

print(search('.'))