Create a tree-style directory listing in Python

后端 未结 1 879
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 13:42

I am trying to list directories and files (recursivley) in a directory with python:

./rootdir
  ./file1.html
  ./subdir1
    ./file2.html
    ./file3.html
           


        
相关标签:
1条回答
  • 2021-01-13 14:28

    Try the following:

    for path, dirs, files in os.walk("."):
        print path
        for file in files:
            print os.path.join(path, file)
    

    You do not need to print entries from dirs because each directory will be visited as you walk the path, so you will print it later with print path.

    0 讨论(0)
提交回复
热议问题