Python append multiple files in given order to one big file

前端 未结 10 826
时光说笑
时光说笑 2020-11-29 06:00

I have up to 8 seperate Python processes creating temp files in a shared folder. Then I\'d like the controlling process to append all the temp files in a certain order into

10条回答
  •  青春惊慌失措
    2020-11-29 06:32

    In this code, you can indicate the path and name of the input/output files, and it will create the final big file in that path:

    import os
    
    dir_name = "Your_Desired_Folder/Goes_Here"    #path
    input_files_names = ["File1.txt", "File2.txt", "File3.txt"]     #input files
    file_name_out = "Big_File.txt"     #choose a name for the output file
    file_output = os.path.join(dir_name, file_name_out)
    fout = open(file_output, "w")
    
    for tempfile in input_files_names:
        inputfile = os.path.join(dir_name, tempfile)
        fin = open(inputfile, 'r')
        for line in fin:
            fout.write(line)
    
    fin.close()    
    fout.close()
    

提交回复
热议问题