美文网首页
Python读取文件夹中TXT文档

Python读取文件夹中TXT文档

作者: qthink | 来源:发表于2017-02-11 17:58 被阅读0次

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()

相关文章

网友评论

      本文标题:Python读取文件夹中TXT文档

      本文链接:https://www.haomeiwen.com/subject/xzjoittx.html