Python append multiple files in given order to one big file

前端 未结 10 829
时光说笑
时光说笑 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:34

    Use fileinput:

    with open("bigfile.txt", "w") as big_file:
        with fileinput.input(files=tempfiles) as inputs:
            for line in inputs:
                big_file.write(line)
    

    This is more memory efficient than @RafeKettler's answer as it doesn't need to read the whole file into memory before writing to big_file.

提交回复
热议问题