Files
spark/tools/file_utils.py
T
John Colvin 1b9ac61588 Add save-log script to backup log file
The current date and time is appended to the log file name
2022-01-22 12:46:52 -05:00

34 lines
884 B
Python

import os
import datetime
def fmt_datetime_suffix(dt: datetime.datetime = None):
return (dt or datetime.datetime.now()).strftime("%Y-%m%d-%H%M")
def append_datetime_suffix(path, dt: datetime.datetime = None):
return f'{path}_{fmt_datetime_suffix(dt)}'
def get_unique_path(
path: str, max_tries: int = 100):
if path is None or len(path) == 0:
raise ValueError("'path' is none or empty")
if not os.path.exists(path):
return path
max_tries = 100
i = 1
orig_path = path
while i < max_tries:
path = f'{orig_path}({i})'
if not os.path.exists(path):
break
i += 1
if i == max_tries:
raise ValueError(
f"Unable to make path unique by appending appending an incrementing integer. "
f"Maximum attempts exceeded ({max_tries})")
return path