List Files and Directory Tree Structure in Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os


def walk(path):
l = []
file_dir_list = os.listdir(path)
if not file_dir_list:
return []
for item in file_dir_list:
if os.path.isfile(path + "\\" + item):
l.append(item)
else:
l += walk(path + "\\" + item)

return l


def dfs_show_dir(path, depth):
if depth == 0:
print("root: [" + path + "]")

for item in os.listdir(path):
print("| " * depth + "+--" + item)

new_item = path + "/" + item
if os.path.isdir(new_item):
dfs_show_dir(new_item, depth + 1)


if __name__ == "__main__":
path = os.getcwd()
os.chdir("C:\\Users\\lixiang\\PycharmProjects\\other\\testcase_for_walk")
path = os.getcwd()
print(walk(path), "\n")

dfs_show_dir(path, 0)

1
2
3
4
5
6
7
8
9
['4.txt', '5.txt', '6.txt'] 

root: [C:\Users\lixiang\PycharmProjects\other\testcase_for_walk]
+--1
| +--2
| +--3
| | +--4.txt
| | +--5.txt
| +--6.txt