#coding=utf-8 import os import csv file_list = os.walk("e:\英语学习\词汇词根+联想记忆法29本汇总") files = [a for a in file_list] print(type(files)) print(files) with open("d:/files.csv","w") as f: f.writelines(files) #上面报错原因:因为walk后产生了3个元组,这里的元组是不能被writelines的。要拆解元组
****************************************************************************************************************
import os file_list = os.walk("e:\英语学习\词汇词根+联想记忆法29本汇总") #files = [a for a in file_list] #print(type(files)) #print(files) #with open("d:/files.csv","w") as f: # f.writelines(files) #上面报错原因:因为walk后产生了3个元组,这里的元组是不能被writelines的。要拆解元组 allfiles = [] for dirpath,dirnames,filenames in file_list: for dir in dirnames: allfiles.append(os.path.join(dirpath,dir)) #这里应该写join(dirpath,dir),但我不想要路径,可删除dirpath for file in filenames: allfiles.append(os.path.join(file)) #同理 join(dirpath,file) with open("d:/files.csv","w") as f: file2 = [a+"\n" for a in allfiles] f.writelines(file2) print(file2)