summaryrefslogtreecommitdiff
path: root/Code/log.py
blob: 396acdd757aa25705d573eb815bc14ba6fb86ff9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import os
from datetime import datetime

def write_log_file(s, fname):
    ''' Write s to fname, the most recent on the top'''
    curr_time = datetime.now().strftime('%Y-%m-%d %H:%M')
    print('Log at %s: %s' % (curr_time, s.strip()))
    content = ''
    if os.path.exists(fname):
        with open(fname) as f:
            content = f.read()
    with open(fname, 'w') as f:
        f.write('[' + curr_time + ']: ' + s.strip() + '\n' + content)



if __name__ == '__main__':
    write_log_file('hello', 'testlog.txt');
    write_log_file('logger', 'testlog.txt');