-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos_utils.py
More file actions
36 lines (27 loc) · 1.02 KB
/
os_utils.py
File metadata and controls
36 lines (27 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import pickle
from logging import Logger
def create_folder_if_not_exists(directory) -> None:
try:
os.makedirs(directory, exist_ok=True)
print(f"Directory '{directory}' created or already exists.")
except Exception as e:
print(f"An error occurred: {e}")
def read_file(filename: str, logger: Logger) -> dict:
messages = {}
if not os.path.exists(filename):
return messages
try:
with open(filename, 'rb') as f:
messages = pickle.load(f)
logger.info(f"Loaded data from local file {os.path.join(os.path.curdir, filename)}!")
except Exception as e:
logger.error(f"Failed to load data from file: {e}")
return messages
def write_file(data, filename, logger: Logger) -> None:
try:
with open(filename, 'wb') as f:
pickle.dump(data, f)
logger.info(f"Wrote data to local file {os.path.join(os.path.curdir, filename)}!")
except Exception as e:
logger.error(f"Failed to write data to file: {e}")