-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.py
More file actions
63 lines (53 loc) · 1.93 KB
/
FileManager.py
File metadata and controls
63 lines (53 loc) · 1.93 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy as np
import pickle
import os
class FileManager():
def __init__(self, root):
"""
root (str): The root directory from which this FileManager works.
"""
self.root = root
os.makedirs(root, exist_ok=True)
self.filepath = self.root
def set_filepath(self, *paths):
"""
Set the current filepath relative to the root directory. Helpful for temporarily
going into a subdirectory.
*paths (str): Variable number of path components to join.
"""
self.filepath = os.path.join(self.root, *paths)
os.makedirs(self.filepath, exist_ok=True)
def get_filename(self, fn):
"""
Get the absolute file path given a filename relative to the current filepath.
fn (str): The filename relative to the current filepath.
"""
return os.path.join(self.filepath, fn)
def save(self, obj, fn):
"""
Store an object to disk.
obj (object): The object to be saved.
fn (str): The filename relative to the current filepath. Should end in .npy if obj is ndarray.
"""
fn = self.get_filename(fn)
if fn.endswith('.npy'):
assert isinstance(obj, np.ndarray)
np.save(fn, obj)
return
with open(fn, 'wb') as handle:
pickle.dump(obj, handle, protocol=pickle.HIGHEST_PROTOCOL)
def load(self, fn):
"""
Load an object from disk.
fn (str): The filename relative to the current filepath.
Returns: The loaded object, or None if the file does not exist.
"""
fn = self.get_filename(fn)
if not os.path.isfile(fn):
return None
if fn.endswith('.npy'):
obj = np.load(fn)
return obj
with open(fn, 'rb') as handle:
obj = pickle.load(handle)
return obj