我正在使用递归扫描目录树的函数。除了 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.curdir
和 os.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
模块中找不到执行此操作的内容。
最佳答案
原来我原来的函数有两个问题:
- 它使用
path
作为参数,与os
的path
子模块产生名称冲突。 - 我应该使用
os.path.abspath(path_to_file)
而不是os.curdir
和os.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/