SoFunction
Updated on 2024-11-07

Python method to achieve the same folder to merge all the txt file example

In this article, examples of Python to achieve the merger of all txt files in the same folder. Shared for your reference, as follows:

I. Needs analysis

Merge all txt files in a folder

II. Merger effects

III. python implementation code

# -*- coding:utf-8*-
import sys
reload(sys)
('utf-8')
import os
import 
import time
time1=()
########################## Merge multiple txts in the same folder ################
def MergeTxt(filepath,outfile):
  k = open(filepath+outfile, 'a+')
  for parent, dirnames, filenames in (filepath):
    for filepath in filenames:
      txtPath = (parent, filepath) # txtpath is the path to all folders
      f = open(txtPath)
      ########## line feeds written to ##################
      (()+"\n")
  ()
  print "finished"
if __name__ == '__main__':
  filepath="D:/course/"
  outfile=""
  MergeTxt(filepath,outfile)
  time2 = ()
  print u'Total time spent:' + str(time2 - time1) + 's'

Run results:

"D:\Program Files\Python27\" D:/PycharmProjects/learn2017/ Merge multiple
finished
Total time consumed: 0.000999927520752s
Process finished with exit code 0

For more Python related content, readers can check out this site's topic:Summary of Python text file manipulation techniques》、《Summary of Python file and directory manipulation techniques》、《Summary of Python coding manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.