python读取文件夹下所有txt_Python读取文件夹中TXT文档

1.读取文件夹中多个txt文档

dest = "sougou" #文档的路径

for root, dirs, files in os.walk(dest):

for OneFileName in files:

if OneFileName.find('.txt') == -1:

continue

OneFullFileName = join(root, OneFileName)#获取所有文件

print(OneFileName)

wd="sougou/"+OneFileName

f=open(wd,'r+')

raw=f.read()

raw_documents.append(raw)

2.将一个txt文件存储成多个txt文档

f=open('source/数学教案.txt','r')

name=1

for line in f:

line_w=line.strip()

finename='jiaoan/'+str(name)+'.txt'

f1=open(finename,'w+')

f1.write(line_w)

name+=1

f1.close()

f.close()