Combining multiple text files into one with python

arturo823 :

I am looking to put a bunch of text files into one directory, then create a new text file with all the files combined. Any help would be great!

Yusuf Berki Yazıcıoğlu :

I think you want to put the txt files in a folder into a single txt file. You can fix the paths and try this code:

import os

text_file_path = "text_files"
text_files = [os.path.join(text_file_path,x) for x in os.listdir(text_file_path)]
output_file = open("output.txt","w")

for text_file in text_files:
    for line in open(text_file).readlines():
        output_file.write(line)
output_file.close()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=367801&siteId=1