Files
OpenECAD_Project/Bethany/lib/file_utils.py
2024-08-16 17:50:51 +08:00

53 lines
1010 B
Python

import os
import json
import logging
import shutil
import csv
def save_args(args, save_dir):
param_path = os.path.join(save_dir, 'params.json')
with open(param_path, 'w') as fp:
json.dump(args.__dict__, fp, indent=4, sort_keys=True)
def ensure_dir(path):
"""
create path by first checking its existence,
:param paths: path
:return:
"""
if not os.path.exists(path):
os.makedirs(path)
def ensure_dirs(paths):
"""
create paths by first checking their existence
:param paths: list of path
:return:
"""
if isinstance(paths, list) and not isinstance(paths, str):
for path in paths:
ensure_dir(path)
else:
ensure_dir(paths)
def remkdir(path):
"""
if dir exists, remove it and create a new one
:param path:
:return:
"""
if os.path.exists(path):
shutil.rmtree(path)
os.makedirs(path)
def cycle(iterable):
while True:
for x in iterable:
yield x