-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_ops.py
35 lines (31 loc) · 892 Bytes
/
file_ops.py
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 pathlib
def read(fpath: pathlib.Path) -> str:
contents = ""
if fpath.exists():
contents = fpath.read_text()
return contents
def mkdir(dir_path: pathlib.Path):
'''
Creates the dir if it doesn't already exist
'''
if not dir_path.exists():
os.system(f"mkdir {dir_path}")
def mkdir_rec(main_dir_path: pathlib.Path):
'''
Creates the dir path to `main_dir_path` starting from the deepest one not in existence
'''
parents = main_dir_path.parents
for dir_path in parents:
mkdir(dir_path)
def write(fpath: pathlib.Path, contents: str = ""):
'''
Make dir path and write `contents` to `fpath`
'''
if not fpath.parent.exists():
mkdir_rec(fpath.parent)
if not fpath.exists():
os.system(f"touch {fpath}")
else:
os.unlink(str(fpath))
fpath.write_text(contents)