相关疑难解决方法(0)

Python列表目录,子目录和文件

我正在尝试创建一个脚本来列出给定目录中的所有目录,子目录和文件.
我试过这个:

import sys,os

root = "/home/patate/directory/"
path = os.path.join(root, "targetdirectory")

for r,d,f in os.walk(path):
    for file in f:
        print os.path.join(root,file)
Run Code Online (Sandbox Code Playgroud)

不幸的是,它无法正常工作.
我得到了所有文件,但不是完整的路径.

例如,如果dir结构是:

/home/patate/directory/targetdirectory/123/456/789/file.txt

它会打印:

/home/patate/directory/targetdirectory/file.txt

我需要的是第一个结果.任何帮助将不胜感激!谢谢.

python file path

105
推荐指数
7
解决办法
16万
查看次数

标签 统计

file ×1

path ×1

python ×1