当前第2页 返回上一页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import sys
reload (sys)
sys.setdefaultencoding( 'utf-8' )
import os
import os.path
import time
time1 = time.time()
def MergeTxt(filepath,outfile):
k = open (filepath + outfile, 'a+' )
for parent, dirnames, filenames in os.walk(filepath):
for filepath in filenames:
txtPath = os.path.join(parent, filepath)
f = open (txtPath)
k.write(f.read() + "\n" )
k.close()
print "finished"
if __name__ = = '__main__' :
filepath = "D:/course/"
outfile = "result.txt"
MergeTxt(filepath,outfile)
time2 = time.time()
print u '总共耗时:' + str (time2 - time1) + 's'
|
运行结果:
"D:\Program Files\Python27\python.exe" D:/PycharmProjects/learn2017/合并多个txt.py
finished
总共耗时:0.000999927520752s
Process finished with exit code 0
更多Python相关内容感兴趣的读者可查看本站专题:《Python文本文件操作技巧汇总》、《Python文件与目录操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
相关推荐:
python实现对指定输入的字符串逆序输出的方法
Python实现按当前日期(年、月、日)创建多级目录的方法
Python实现的计算器功能
以上就是Python实现合并同一个文件夹下所有txt文件的方法的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
Python的int是什么
Python的除法运算符是什么意思
Python怎么输入变量
Python拿什么和java比
Python装饰器之property用法详解
Python如何实现excel多表合并(附代码)
Python有double类型吗
Python的元类如何使用
Python与access选哪个
Python闭包是什么?Python闭包的简单介绍(附示例)
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python实现合并同一个文件夹下所有txt文件的方法