Python append multiple files in given order to one big file

前端 未结 10 806
时光说笑
时光说笑 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:40
    import os
    str = os.listdir("./")
    
    for i in str:
        f = open(i)
        f2 = open("temp.txt", "a")
        for line in f.readlines():
            f2.write(line)
    

    We can use above code to read all the contents from all the file present in current directory and store into temp.txt file.

    0 讨论(0)
  • 2020-11-29 06:47

    Rafe's answer was lacking proper open/close statements, e.g.

    # tempfiles is a list of file handles to your temp files. Order them however you like
    with open("bigfile.txt", "w") as fo:
         for tempfile in tempfiles:
              with open(tempfile,'r') as fi: fo.write(fi.read())
    

    However, be forewarned that if you want to sort the contents of the bigfile, this method does not catch instances where the last line in one or more of your temp files has a different EOL format, which will cause some strange sort results. In this case, you will want to strip the tempfile lines as you read them, and then write consistent EOL lines to the bigfile (i.e. involving an extra line of code).

    0 讨论(0)
  • 2020-11-29 06:48

    Just using simple file IO:

    # tempfiles is a list of file handles to your temp files. Order them however you like
    f = open("bigfile.txt", "w")
    for tempfile in tempfiles:
        f.write(tempfile.read())
    

    That's about as OS agnostic as it gets. It's also fairly simple, and the performance ought to be about as good as using anything else.

    0 讨论(0)
  • 2020-11-29 06:50

    Not aware of any shell-level commands for appending one file to another. But appending at 'python level' is sufficiently easy that I am guessing python developers did not think it necessary to add it to the library.

    The solution depends on the size and structure of the temp files you are appending. If they are all small enough that you don't mind reading each of them into memory, then the answer from Rafe Kettler (copied from his answer and repeated below) does the job with the least amount of code.

    # tempfiles is an ordered list of temp files (open for reading)
    f = open("bigfile.txt", "w")
    for tempfile in tempfiles:
        f.write(tempfile.read())
    

    If reading files fully into memory is not possible or not an appropriate solution, you will want to loop through each file and read them piece-wise. If your temp file contains newline-terminated lines which can be read individually into memory, you might do something like this

    # tempfiles is an ordered list of temp files (open for reading)
    f = open("bigfile.txt", "w")
    for tempfile in tempfiles:
        for line in tempfile
            f.write(line)
    

    Alternatively - something which will always work - you may choose a buffer size and just read the file piece-wise, e.g.

    # tempfiles is an ordered list of temp files (open for reading)
    f = open("bigfile.txt", "w")
    for tempfile in tempfiles:
        while True:
            data = tempfile.read(65536)
            if data:
                f.write(data)
            else:
                break
    

    The input/output tutorial has a lot of good info.

    0 讨论(0)
提交回复
热议问题