python - 在递归函数中获取扫描目录和文件的绝对路径?

标签 python linux python-3.x recursion operating-system

我正在使用递归扫描目录树的函数。除了 1 个主要细节外,它的工作非常出色。我的目录树如下所示:

/testdir--|--folder1--|--pop----quit.mp3
          |           |_drift.mp3
          |           |_drifting.mp3  
          |__folder2----rock--|--paranoid.mp3
                              |__countdown.mp3 

我现在的代码是这样的:

def craw_func(path):
    """
        Scans the directory recursively, returning a JSON with name,
        type,path and children.
    """
    d = {'name': os.path.basename(path)} 

    if os.path.isdir(path):
        d['type'] = "directory"
        d['path'] = os.curdir # NOT WORKING
        d['children'] = [craw_func(os.path.join(path,x)) for x in os.listdir(path)]
    else:
        d['type'] = "file"
        d['path'] = os.getcwd()  # NOT WORKING EITHER

    return d

正如您从我的评论中看到的那样,我在不同的位置尝试了几次,但 os.curdiros.getcwd() 都没有给出真实的(绝对)我想要的路径,两者都只返回脚本运行的目录。给这个:

{
    "children": [
        {
            "children": [
                {
                    "children": [
                        {
                            "name": "paranoid.mp3",
                            "path": "/home/myname/project_folder/",
                            "type": "file"
                        },
                        {
                            "name": "countdown.mp3",
                            "path": "/home/myname/project_folder/",
                            "type": "file"
                        }
                    ],
                    "name": "rock",
                    "path": ".",
                    "type": "directory"
                }
            ],
            "name": "folder2",
            "path": ".",
            "type": "directory"
        },
        {
            "children": [
                {
                    "name": "drift.mp3",
                    "path": "/home/myname/project_folder/",
                    "type": "file"
                },
                {
                    "children": [
                        {
                            "name": "quit.mp3",
                            "path": "/home/myname/project_folder/",
                            "type": "file"
                        }
                    ],
                    "name": "pop",
                    "path": ".",
                    "type": "directory"
                },
                {
                    "name": "drifting.mp3",
                    "path": "/home/myname/project_folder/",
                    "type": "file"
                }
            ],
            "name": "folder1",
            "path": ".",
            "type": "directory"
        }
    ],
    "name": "test_dir",
    "path": ".",
    "type": "directory"
}

而不是这个(我的目标):

{
    "children": [
        {
            "children": [
                {
                    "children": [
                        {
                            "name": "paranoid.mp3",
                            "path": "/home/myname/test_dir/folder2/rock",
                            "type": "file"
                        },
                        {
                            "name": "countdown.mp3",
                            "path": "/home/myname/test_dir/folder2/rock/",
                            "type": "file"
                        }
                    ],
                    "name": "rock",
                    "path": "/home/myname/test_dir/folder2/",
                    "type": "directory"
                }
            ],
            "name": "folder2",
            "path": "/home/myname/test_dir/",
            "type": "directory"
        },
        {
            "children": [
                {
                    "name": "drift.mp3",
                    "path": "/home/myname/test_dir/folder1/",
                    "type": "file"
                },
                {
                    "children": [
                        {
                            "name": "quit.mp3",
                            "path": "/home/myname/test_dir/folder1/pop/",
                            "type": "file"
                        }
                    ],
                    "name": "pop",
                    "path": "/home/myname/test_dir/folder1/",
                    "type": "directory"
                },
                {
                    "name": "drifting.mp3",
                    "path": "/home/myname/test_dir/folder1/",
                    "type": "file"
                }
            ],
            "name": "folder1",
            "path": "/home/myname/test_dir/",
            "type": "directory"
        }
    ],
    "name": "test_dir",
    "path": "/home/myname/",
    "type": "directory"
}

我应该使用什么来让它按照我想要的方式运行?我进行了搜索,但在 os 模块中找不到执行此操作的内容。

最佳答案

原来我原来的函数有两个问题:

  1. 它使用 path 作为参数,与 ospath 子模块产生名称冲突。
  2. 我应该使用 os.path.abspath(path_to_file) 而不是 os.curdiros.getcwd()(如由 @martineau 指出)。

所以我的最终函数看起来像这样:

def craw_func(path_to_file):
    """
        Scans the directory recursively, returning a JSON with name,
    type,path and children.
    """
    d = {'name': os.path.basename(path_to_file)} 

    if os.path.isdir(path_to_file):
        d['type'] = "directory"
        d['path'] = os.path.abspath(path_to_file)
        d['children'] = [craw_func(os.path.join(path_to_file,x)) for x in os.listdir(path_to_file)]
    else:
        d['type'] = "file"
        d['path'] = os.path.abspath(path_to_file)
    return d

关于python - 在递归函数中获取扫描目录和文件的绝对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44076126/

相关文章:

当一个返回结果时,Python 停止多个进程?

python - 如何将 time.strptime 用于充满长格式日期的文件

linux - 运行 "git svn"需要安装哪些依赖项才能正常运行?

linux - Apache2 网络服务器不显示页面

python - 如何获取不在整数黑名单中的随机数?

python - 'from flask import Flask' 引发语法错误 - Python

linux - 在 vim 中输入过滤命令后将光标移动到下一行

python - 某些数据未存储在数组中

python - 如何从文件中读取数据并获取索引输出

python - 如何通过tesseract OCR读取黑色背景图像上的黑色文本?