@@ -41,11 +41,10 @@ def _check_saving_path(path_to_file, verbose=False, print_prefix="", state_verb=
41
41
>>> from pyhelpers.store import _check_saving_path
42
42
>>> from pyhelpers.dirs import cd
43
43
>>> path_to_file = cd()
44
- >>> try:
45
- ... _check_saving_path(path_to_file, verbose=True)
46
- ... except AssertionError as e:
47
- ... print(e)
48
- The input for `path_to_file` may not be a file path.
44
+ >>> _check_saving_path(path_to_file, verbose=True)
45
+ Traceback (most recent call last):
46
+ ...
47
+ AssertionError: The input for `path_to_file` may not be a file path.
49
48
>>> path_to_file = "pyhelpers.pdf"
50
49
>>> _check_saving_path(path_to_file, verbose=True); print("Passed.")
51
50
Saving "pyhelpers.pdf" ... Passed.
@@ -63,17 +62,11 @@ def _check_saving_path(path_to_file, verbose=False, print_prefix="", state_verb=
63
62
abs_path_to_file = pathlib .Path (path_to_file ).absolute ()
64
63
assert not abs_path_to_file .is_dir (), "The input for `path_to_file` may not be a file path."
65
64
66
- filename = pathlib .Path (abs_path_to_file ).name if abs_path_to_file .suffix else ""
67
-
68
65
try :
69
- rel_dir_path = pathlib .Path ( os . path . relpath ( abs_path_to_file . parent ))
66
+ rel_dir_path = abs_path_to_file . parent . relative_to ( pathlib .Path . cwd ( ))
70
67
71
- if rel_dir_path .is_relative_to ("." ):
72
- pass
73
- elif rel_dir_path == rel_dir_path .parent :
68
+ if rel_dir_path .is_relative_to ("." ) and rel_dir_path == rel_dir_path .parent :
74
69
rel_dir_path = abs_path_to_file .parent
75
- else : # In case the specified path does not exist
76
- os .makedirs (abs_path_to_file .parent , exist_ok = True )
77
70
78
71
except ValueError :
79
72
if verbose == 2 :
@@ -83,13 +76,17 @@ def _check_saving_path(path_to_file, verbose=False, print_prefix="", state_verb=
83
76
84
77
rel_dir_path = abs_path_to_file .parent
85
78
79
+ rel_dir_path .mkdir (parents = True , exist_ok = True ) # In case the specified path does not exist
80
+
81
+ filename = abs_path_to_file .name if abs_path_to_file .suffix else ""
82
+
86
83
if verbose :
87
- if os .path .exists (abs_path_to_file ):
84
+ if os .path .isfile (abs_path_to_file ):
88
85
state_verb , state_prep = "Updating" , "at"
89
86
90
87
end = print_end if print_end else "\n "
91
88
92
- if (rel_dir_path == rel_dir_path .parent and
89
+ if (rel_dir_path == rel_dir_path .parent or rel_dir_path == abs_path_to_file . parent ) and (
93
90
rel_dir_path .absolute ().drive == pathlib .Path .cwd ().drive ):
94
91
msg = f'{ print_prefix } { state_verb } "{ filename } "{ print_suffix } '
95
92
print (msg , end = end )
0 commit comments