Welcome, guest | Sign In | My Account | Store | Cart

This recipe demonstrates how easy it is to compile a list of (log) files into one large file. The source files come from the current directory, and the output file is written to the current directory. Only files with a specific format are put into the compiled (log) file.

Python, 13 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import os, time

def main():
    master = dict()
    for name in os.listdir(os.getcwd()):
        try:
            master[time.mktime(time.strptime(name, '%A, %B %d, %Y.txt'))] = name
        except:
            pass
    file('Logbook.txt', 'w').write('\n'.join([file(master[index]).read() for index in sorted(master)]))

if __name__ == '__main__':
    main()

If you have a directory with a number of log files and want to combine those files into one file (for whatever reason), this is a very useful recipe if it can automate that task. In my case, I had 22 files that will increase in number and that will need to be compiled for printing every so often. It was for this reason that the recipe shown here was created.