This repository was archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjutils.py
More file actions
61 lines (47 loc) · 1.37 KB
/
jutils.py
File metadata and controls
61 lines (47 loc) · 1.37 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
import io
import os
"""
Misc helpful utils, mainly related to File IO
@author Jeff Chen
@version 6/15/2022
"""
def write_utf8(text:str, path:str, mode:str) -> None:
"""
Writes utf-8 text to a file at path
Param:
text: text to write
path: where file to write to is located including file name
mode: mode to set FIle IO
"""
with io.open(path, mode=mode, encoding='utf-8') as fd:
fd.write(text)
def write_to_file(path:str, line: str, mutex) -> None:
"""
Appends to a file, creates the file if it does not exists
Param:
path: file to write to, absolute path
line: line to append to file
mutex: (Optional) mutex lock associated with the file
"""
if mutex:
mutex.acquire()
write_utf8(line, path, 'a')
#if not os.path.exists(path):
# open(path, 'a').close()
#with open(fname, "a") as myfile:
# myfile.write(line)
if mutex:
mutex.release()
def getDirSz(dir: str) -> int:
"""
Returns directory and its content size
Return directory and its content size
"""
size = 0
for dirpath, dirname, filenames in os.walk(dir):
for f in filenames:
fp = os.path.join(dirpath, f)
# skip if it is symbolic link
if not os.path.islink(fp):
size += os.path.getsize(fp)
return size