import csv import os # 寻找程序所在目录下的所有文本文件,并打印文件名 def scan_files(filepath): # 传入当前绝对路径及指定字符串 # 用于清空filename.csv'内容 with open('filename.csv', 'w', encoding='utf-8', newline='') as file_handler: file_writer = csv.writer(file_handler) row = ['file_no', 'filename'] file_writer.writerow(row) files = os.listdir(filepath) file_no = 0 # 遍历所有文件 print('files list:') for filename in files: file_no = file_no + 1 print('%d--' % file_no, end='') print(filename) # 保存文件名到filename.csv' with open('filename.csv', 'a+', encoding='utf-8', newline='') as file_handler: file_writer = csv.writer(file_handler) row = [file_no, filename] file_writer.writerow(row) return file_no