title: Combining Log Files and Sorting by Time in Python
body: <p>Firstly, I'm very new to Python and programming in general so please bear with me if this is a stupidly obvious question.</p>
  
  <p>I have an undefined amount (possibly 10+) of log files mixed with other random files in a directory, and I need to merge these into a single file with the lines sorted by the time stamp at the beginning of each line. The log files are .txt and there are other non-log .txt files in the same directory so I'm going to just make the user of this script enter every log file as an argument.</p>
  
  <p>Now before you mark this as a duplicate, I looked through 4 pages of search results on here and <strong>none</strong> of the questions have an answer I can use.</p>
  
  <p>So far, I have the following sort-of working Python code:</p>
  
  <pre><code>log_file_name = 'logfile.txt'
  
  import sys
  import fileinput
  from Tkinter import Tk
  from tkFileDialog import askopenfilenames
  
  logfile = open(log_file_name, 'w+')
  logfile.truncate()
  logfile.seek(0)
  
  # get list of file names
  print "Opening File Dialog"
  Tk().withdraw()
  files = askopenfilenames(title='Select all logs you would like to compile.')
  
  for index in range(len(files)):
      print "Loop ", index
      print "--- Debug message: Reading a file... ---"
      logdata = open((files[index])).readlines()
      print "--- Debug message: Finished reading. Writing a file... ---"
      # turns logdata into a string and writes it to logfile
      logfile.write(''.join(logdata))
      logfile.write("\n")
  
  print ""
  print "Exited for loop."
  logfile.close()
  </code></pre>
  
  <p>The above code puts the contents of all the files you select into a single text file, but it doesn't sort them.</p>
  
  <p>I was thinking of using regex to search for numbers inside of brackets and then sort each line based on that...?</p>
  
  <p>Here are some sample log files contents.</p>
  
  <pre><code>[xx.xxxxxx] [Text] Text : Text: xxx
  [xx.xxxxxx] [Text] Text : Text: xxx
  [xx.xxxxxx] [Text] Some text.
  There could be multiple lines of text here
  These lines could include [brackets.] :(
  
  [xx.xxxxxx] [Text] Text : Text: xxx
  </code></pre>
  
  <p>The [xx.xxxxxx] is the time stamp in seconds since system startup.</p>