summaryrefslogtreecommitdiff
path: root/Code/log.py
diff options
context:
space:
mode:
authorHui Lan <lanhui@zjnu.edu.cn>2024-08-05 14:44:44 +0800
committerHui Lan <lanhui@zjnu.edu.cn>2024-08-05 14:44:44 +0800
commitdf4af8751a5c1e1465159083100b82e58e7778f7 (patch)
tree4962f84f789520f3e3be54e94ff11f06d5e35e8f /Code/log.py
parent0d5e23b870ef9fa21cc90e31150dc1c27d56b3f8 (diff)
Update write_log_file() so that it put the most recent message on the top; remove duplicate write_log_file()
Diffstat (limited to 'Code/log.py')
-rw-r--r--Code/log.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/Code/log.py b/Code/log.py
new file mode 100644
index 0000000..396acdd
--- /dev/null
+++ b/Code/log.py
@@ -0,0 +1,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');