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

时间:2017-05-19 17:47:54

标签: 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 个答案:

答案 0 :(得分:0)

原来我的原始功能有2个问题:

  1. 使用path作为参数,使用path的{​​{1}}子模块创建名称冲突。
  2. 我应该使用os代替os.path.abspath(path_to_file)os.curdir(由@martineau指出)。
  3. 所以我的最终功能看起来像这样:

    os.getcwd()