Home > Backend Development > Python Tutorial > Python implements the method of merging all txt files in the same folder

Python implements the method of merging all txt files in the same folder

不言
Release: 2018-04-26 16:44:35
Original
8769 people have browsed it

This article mainly introduces Python's method of merging all txt files in the same folder, involving Python's operating skills for file traversal, reading, writing and other related operations. Friends who need it can refer to it

The example in this article describes how Python implements the method of merging all txt files in the same folder. Share it with everyone for your reference, the details are as follows:

1. Requirements analysis

Merge all txt files in one folder

##2. Merging effect

3. python implementation code

# -*- coding:utf-8*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import os
import os.path
import time
time1=time.time()
##########################合并同一个文件夹下多个txt################
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) # txtpath就是所有文件夹的路径
      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'
Copy after login

Run result:

" D:\Program Files\Python27\python.exe" D:/PycharmProjects/learn2017/Merge multiple txt.py

finished
Total time taken: 0.000999927520752s
Process finished with exit code 0

Readers who are interested in more Python related content can check out the special topics on this site: "Summary of Python text file operation skills", "Summary of Python file and directory operation skills", "Summary of Python coding operation skills", "Python Data Structure and Algorithm Tutorial", "Python Function Usage Skills Summary", Python String Operation Skills Summary and "Python Introduction and Advanced Classic Tutorial"

Related recommendations:

Python implements the method of outputting the specified input string in reverse order

Python implements the method of creating a multi-level directory based on the current date (year, month, day)

Calculator function implemented in Python

The above is the detailed content of Python implements the method of merging all txt files in the same folder. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template