-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
151 lines (104 loc) · 3.57 KB
/
utils.py
File metadata and controls
151 lines (104 loc) · 3.57 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import hashlib
import os
import os.path
import pathlib
import typing
from typing import List, Tuple
def md5_for_file(path_to_file: str) -> str:
with open(path_to_file, 'rb') as f:
return hashlib.md5(f.read()).hexdigest()
def get_file_size(path_to_file: str) -> int:
return os.path.getsize(path_to_file)
def path_get_mtime(path: str) -> float:
return os.path.getmtime(path)
def path_split_ext(file_path: str) -> Tuple[str, str]:
return os.path.splitext(file_path)
def path_is_file(path_to_file: str) -> bool:
path = pathlib.Path(path_to_file)
return path.is_file()
def path_is_directory(path_to_dir: str) -> bool:
path = pathlib.Path(path_to_dir)
return path.is_dir()
def file_exists(path_to_file: str) -> bool:
return os.path.exists(path_to_file) and path_is_file(path_to_file)
def rename_file(old_path_to_file: str, new_path_to_file: str) -> bool:
try:
os.rename(old_path_to_file, new_path_to_file)
return True
except:
return False
def delete_file(path_to_file: str) -> bool:
if file_exists(path_to_file):
os.remove(path_to_file)
return True
else:
return False
def directory_exists(path_to_dir: str) -> bool:
return os.path.exists(path_to_dir) and path_is_directory(path_to_dir)
def path_join(dir_path: str, file_name: str) -> str:
return os.path.join(dir_path, file_name)
def os_is_posix() -> bool:
return os.name == 'posix'
def list_files_in_directory(dir_path: str) -> List[str]:
file_list = []
if directory_exists(dir_path):
list_entries = os.listdir(dir_path)
for entry in list_entries:
file_path = os.path.join(dir_path, entry)
entry_is_file = path_is_file(file_path)
if entry_is_file and entry not in ['.', '..']:
file_list.append(entry)
return file_list
def list_dirs_in_directory(dir_path: str) -> List[str]:
dir_list = []
if directory_exists(dir_path):
list_entries = os.listdir(dir_path)
for entry in list_entries:
if path_is_directory(entry):
dir_list.append(entry)
return dir_list
def delete_files_in_directory(dir_path: str) -> bool:
if directory_exists(dir_path):
file_list = list_files_in_directory(dir_path)
for file in file_list:
file_path = os.path.join(dir_path, file)
delete_file(file_path)
return True
else:
return False
def get_current_directory() -> str:
return os.getcwd()
def get_process_id() -> int:
return os.getpid()
def create_directory(dir_path: str) -> bool:
os.makedirs(dir_path)
return directory_exists(dir_path)
def delete_directory(dir_path: str) -> bool:
os.rmdir(dir_path)
return not directory_exists(dir_path)
def file_write_all_bytes(file_path: str, file_contents: list[bytes]) -> bool:
try:
with open(file_path, "wb") as f:
f.write(file_contents)
return True
except IOError:
return False
def file_write_all_text(file_path: str, file_contents: str) -> bool:
try:
with open(file_path, "w") as f:
f.write(file_contents)
return True
except IOError:
return False
def file_read_all_bytes(file_path: str) -> typing.Optional[list[bytes]]:
try:
with open(file_path, "rb") as f:
return f.read()
except IOError:
return None
def file_read_all_text(file_path: str) -> typing.Optional[str]:
try:
with open(file_path, "r") as f:
return f.read()
except IOError:
return None