first commit
This commit is contained in:
52
lib/file_utils.py
Normal file
52
lib/file_utils.py
Normal file
@@ -0,0 +1,52 @@
|
||||
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
|
||||
Reference in New Issue
Block a user