python os.walk()

 

遍历目录,所有文件拼接路径追加列表

import os
import shutil

work_path = r'D:\WorkSpace\Images'

file_path_list = []

for root, dirs, files in os.walk(work_path):  # root:目录路径; dirs:目录下文件夹列表; files:目录下文件列表
    # print(root)
    # print(dirs)
    # print(files)
    for dir in dirs:
        dir_path = os.path.join(root, dir)  # dir_path:目录路径
    for file in files:
        file_path = os.path.join(root, file)  # file_path:文件路径
        # print(file_path)
        file_path_list.append(file_path)

print(len(file_path_list))

你可能感兴趣的:(python,python)