Python遍历文件夹及其子文件夹中的所有文件

# coding:utf-8
import os

path = "C:\\Users\\15527\\Desktop\\demo"


def get_file(path, file_list):
    if os.path.isfile(path):
        file_list.append(path)
    elif os.path.isdir(path):
        for item in os.listdir(path):
            item_path = os.path.join(path, item)
            get_file(item_path, file_list)
    return file_list


list = get_file(path, [])
for file in list:
    print(file)

你可能感兴趣的:(Python,面试)