-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.py
More file actions
61 lines (51 loc) · 1.89 KB
/
Copy pathutility.py
File metadata and controls
61 lines (51 loc) · 1.89 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
#!/usr/bin/env python3.8
from __future__ import annotations # for using '|' instead of Union() on less than version 3.10
import sys
import subprocess
BEEP_PATH = 'beep-msx.wav'
# /////////////////////////////////////////////////////////////
# // utility
def beep() -> None: # TODO: change command 'mpv' to 'aplay' after making wave data lauding more
output = subprocess.run('mpv --volume=200 {0}'.format(BEEP_PATH), shell=True, capture_output=True, text=True)
def remove_file(file_path: str) -> None:
output = subprocess.run('rm -f {0}'.format(file_path), shell=True, capture_output=True, text=True)
# /////////////////////////////////////////////////////////////
# // helper (exception wrapper)
def read_binary_file(file_path: str) -> bytes|None:
try:
with open(file_path, 'rb') as file:
binary = file.read()
return binary
except FileNotFoundError:
print('{0}: file not found'.format(file_path), file=sys.stderr)
return None
except Exception as e:
print('{0}: {e}: file cannot open'.format(file_path), file=sys.stderr)
return None
def write_binary_file(file_path: str, binary: bytes) -> bool:
try:
with open(file_path, 'wb') as file:
file.write(binary)
return True
except Exception as e:
print('{0}: {1}: file cannot written'.format(file_path, e), file=sys.stderr)
return False
def append_binary_file(file_path: str, binary: bytes) -> bool:
try:
with open(file_path, 'ab') as file:
file.write(binary)
return True
except Exception as e:
print('{0}: {1}: file cannot written'.format(file_path, e), file=sys.stderr)
return False
def read_text_file(file_path: str) -> str|None:
try:
with open(file_path, 'r') as file:
text = file.read()
return text
except FileNotFoundError:
print('{0}: file not found'.format(file_path), file=sys.stderr)
return None
except Exception as e:
print('{0}: {e}: file cannot open'.format(file_path), file=sys.stderr)
return None