python3 用遞歸方法列出所有目錄與文件
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
import os
from time import time
dir_count = 0
file_count = 0
def get_all_dir(path, sp = "|"):
# 得到當前目錄下所有的文件
fills_list = os.listdir(path)
sp += "-"
# 處理每一個文件
for file_name in fills_list:
# 判斷是否是路徑(用絕對路徑)
file_abs_path = os.path.join(path, file_name)
if os.path.isdir(file_abs_path):
global dir_count # 寫了這個global,不知道會不會被開除
dir_count += 1
print(sp, "目錄:",file_name)
get_all_dir(file_abs_path, sp)
else:
global file_count
file_count += 1
print(sp, "普通文件:",file_name)
def main():
# user_dir = r"D:\Py\1704"
user_dir = r"C:\Python36"
t1 = time()
get_all_dir(user_dir)
t2 = time()
print("一共有%d個目錄,%d個文件,耗時%.3f秒" % (dir_count, file_count, t2 - t1))
if __name__ == "__main__":
main()
效果圖如下:

