-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_file.py
58 lines (48 loc) · 1.85 KB
/
my_file.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
def get_path_name_ext_from_full_path(full_path: str) -> str:
splits = full_path.split('/')
path = '' if len(splits) <= 1 else ('/'.join(splits[:-1]) + '/')
splits = splits[-1].split('.')
if len(splits) <= 1:
raise ValueError('Path does not include name or extention: ' + filepath)
name = '.'.join(splits[:-1])
ext = '.' + splits[-1]
return path, name, ext
def get_path_from_full_path(full_path: str) -> str:
splits = full_path.split('/')
path = '' if len(splits) <= 1 else ('/'.join(splits[:-1]) + '/')
return path
def get_name_from_full_path(full_path: str) -> str:
splits = full_path.split('/')[-1].split('.')
if len(splits) <= 1:
raise ValueError('Path does not include name or extention: ' + filepath)
name = '.'.join(splits[:-1])
return name
def get_ext_from_full_path(full_path: str) -> str:
splits = full_path.split('/')[-1].split('.')
if len(splits) <= 1:
raise ValueError('Path does not include name or extention: ' + filepath)
ext = '.' + splits[-1]
return ext
def check_filepath(filepath: str, ext: str = '') -> bool:
if ext:
if (ext[0] != '.'):
raise ValueError('ext to check illegal. Re-enter with \'.\'')
ext_len = len(ext)
if filepath[-ext_len:] != ext:
return False
if not os.path.exists(filepath):
return False
return True
def get_filepath_to_create(filepath: str) -> str:
path, name, ext = get_path_name_ext_from_full_path(filepath)
if path and not os.path.exists(path):
raise ValueError('Path of file to create does not exist: ' + filepath)
i = 0
while True:
suffix = '' if i == 0 else ('(' + str(i) + ')')
new_path = path + name + suffix + ext
if not os.path.exists(new_path):
return new_path
else:
i += 1