代码:
import os from os.path import basename path = "D:\桌面文件\pythoncode\xacisco-blog\文章" #文件夹目录 files= os.listdir(path) #得到文件夹下的所有文件名称 txts = [] for file in files: #遍历文件夹 position = path+'\'+ file #构造路径,"\",其中一个''为转义符 #print (position) print (os.path.splitext(file)[0]) with open(position, "r",encoding='utf-8') as f: #打开文件 data = f.read() #读取文件 print(data) #txts.append(data) #txts = ','.join(txts)#转化为非数组类型 #print (txts)