12345678910111213141516171819202122232425 |
- """
- utility functions for narps analysis
- """
- import os
- from datetime import datetime
- def log_to_file(
- fname, s, flush=False, add_timestamp=True, also_print=True, headspace=0
- ):
- """save string to log file"""
- if flush and os.path.exists(fname):
- os.remove(fname)
- if not isinstance(s, str):
- s = str(s)
- # add spacing before line
- if headspace > 0:
- s = os.linesep * headspace + s
- with open(fname, "a+") as f:
- if also_print:
- print(s)
- f.write(s + os.linesep)
- if flush and add_timestamp:
- f.write(datetime.isoformat(datetime.now()) + 2 * os.linesep)
|