python 合并文件夹下所有子文件夹里的txt 文件
importglobimportosdeftest():path='/Users/apple/Desktop/SMJ/'listing=os.listdir(path)f...
import glob
import os
def test():
path='/Users/apple/Desktop/SMJ/'
listing=os.listdir(path)
for i in listing:
print ('current file is:' + i)
read_files=glob.glob('*.txt')
with open('result.txt', 'wb') as outfile:
for f in read_files:
with open(f,'rb') as infile:
outfile.write(infile.read())
这个应该是进入了SMJ下的所有文件夹,但是出来的合并所有txt的大txt每次都是空的。为什么呢?求助!谢谢!
import glob
import os
def test():
read_files=glob.glob('Users/apple/Desktop/SMJ/*.txt')
with open('result.txt', 'wb') as outfile:
for f in read_files:
with open(f,'rb') as infile:
outfile.write(infile.read()) 展开
import os
def test():
path='/Users/apple/Desktop/SMJ/'
listing=os.listdir(path)
for i in listing:
print ('current file is:' + i)
read_files=glob.glob('*.txt')
with open('result.txt', 'wb') as outfile:
for f in read_files:
with open(f,'rb') as infile:
outfile.write(infile.read())
这个应该是进入了SMJ下的所有文件夹,但是出来的合并所有txt的大txt每次都是空的。为什么呢?求助!谢谢!
import glob
import os
def test():
read_files=glob.glob('Users/apple/Desktop/SMJ/*.txt')
with open('result.txt', 'wb') as outfile:
for f in read_files:
with open(f,'rb') as infile:
outfile.write(infile.read()) 展开
1个回答
展开全部
listdir不是进入而是相当于终端命令ls。。
read_files=glob.glob('/Users/apple/Desktop/SMJ/
*.txt')
就可以了。
read_files=glob.glob('/Users/apple/Desktop/SMJ/
*.txt')
就可以了。
更多追问追答
追问
我刚才试了一下,把代码改成了补充问题中的样子。但还是出来了一个空的txt。求助!
追答
import glob
import os
def test():
read_files=glob.glob('/Users/apple/Desktop/SMJ/*.txt')
with open('/Users/apple/Desktop/SMJ/result.txt', 'wb') as outfile:
for f in read_files:
with open(f,'rb') as infile:
outfile.write(infile.read())
test()
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询