-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_ops.py
More file actions
37 lines (27 loc) · 1.15 KB
/
file_ops.py
File metadata and controls
37 lines (27 loc) · 1.15 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
import os
def create_dirs() -> None:
# create directories for each coding challenge difficulty + retired ones
try:
os.mkdir('Retired')
for n in range(1,8+1): os.mkdir(f'{n}_kyu')
except FileExistsError: print('Directory already exists')
except PermissionError: print('Permission denied unable to create directories'); exit(1)
except Exception as er: print(f'Error occurred {er}'); exit(1)
def cleanup_filename(file_name: str) -> str:
# remove illegal windows filename characters
illegal_characters = [
'#', '%', '&', '{', '}', '\\', '<', '>', '*', '?', '/', ' ', '$', '!', "'", '"', ':', '@', '+', '`', '|', '=',
]
return ''.join([ char for char in file_name if char not in illegal_characters ])
def write_file(path, text) -> True | False:
# Write content to a file in a given path
try:
with open(path, 'w', encoding='utf-8') as file:
os.utime(path, None)
file.write(text)
return True
except FileExistsError:
print(f'File already exists {path}')
except Exception as err:
print(f'Failed to write to {path}, {err}')
return False