当前第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代码
Python求解物理学中的双弹簧质能系统的代码实例
Python利用openpyxl库遍历sheet的实例
Python flask 多对多表查询的实例详解
在 flask 中集成 vue
如何降低Python版本
Python中list可以修改吗
Python正则表达式和re库的相关内容介绍(代码示例)
Python语言的面向对象编程的介绍(附代码)
Python keys函数能做到什么?示例解析
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python实现合并同一个文件夹下所有txt文件的方法