Skip to content

Instantly share code, notes, and snippets.

@VelocityRa
Last active August 18, 2016 14:16
Show Gist options
  • Save VelocityRa/d978d64ac7ea48e607e81824d9e33ceb to your computer and use it in GitHub Desktop.
Save VelocityRa/d978d64ac7ea48e607e81824d9e33ceb to your computer and use it in GitHub Desktop.
Prints last X lines of each .txt in given dir
import os
import time, datetime
from collections import deque
# Python 2.7.x
# Made for the decaf emulator
# Prints last X lines of each log in given directory (current by default)
last_x_lines = 20
log_lines_cutoff = 17 # number of chars to cut from each logfile (the date/time)
logs_path = r"/" # currently it uses all .txt files in the directory it's in (except other logs of its kind)
header_padding = 20
def print_game_header(fp, gamename):
gamelen = len(gamename)
linewidth = header_padding + gamelen
out_lines = ["="*linewidth, ' '*(header_padding/2) + gamename, "="*linewidth]
fp.write('\n'.join(out_lines) + '\n')
cur_dir = os.getcwd()
print "Current directory is " + cur_dir
logs = []
for file_dir in os.listdir(cur_dir + logs_path):
if file_dir.endswith(".txt") and not file_dir.startswith("LogsOutput-"):
logs.append(file_dir)
print 'Found {} logs.'.format(len(logs))
time_formatted = datetime.datetime.now().replace(microsecond=0).isoformat('_').replace(':', '-')[:-3] #sorry
outfile = open("LogsOutput-" + time_formatted + '_' + str(len(logs)) + "Logs.txt", "w")
for log_dir in logs:
infile = open(log_dir, "r")
gamename = log_dir[:-4-log_lines_cutoff]
print "Writing " + gamename
outfile.write('\n')
print_game_header(outfile, gamename)
outfile.write('\n')
for line in deque(infile, maxlen = last_x_lines):
outfile.write(line)
outfile.write('\n'*2)
infile.close()
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment