Showing posts with label pyhton file handling. Show all posts
Showing posts with label pyhton file handling. Show all posts

Saturday, September 22, 2018

How to read and write multiple file into one file in python

here is the code :

 
 file_names = []
 for i in range(1,201):
        file_names.append("D:\\alllllllllllll files\\" + str(i) + ".txt")
        #here alllllllllllll files is in a directory where all files are stored
        #and number i which is in range 1,200 is concatenating   with the file address
with open('output file location ', 'w') as outfile:
    for fname in file_names:
        with open(fname) as infile:
            for line in infile:
                outfile.write(line)
            outfile.write('\n')


 

Suppose you have many files stored in a directory and these files are numbered sequentially like in the below image ->

image of text files