1
1
# %%
2
- __version__ = '1.1.0 '
2
+ __version__ = '1.1.2 '
3
3
4
4
# %% [markdown]
5
5
# # ofilepath Lib (Moving/Copying/Getting Files Information)
@@ -36,7 +36,7 @@ def get_last_file(file_path: str=None, extension: bool=True, file_type: str='',
36
36
37
37
return file
38
38
39
- def get_last_file_name (file_path = None ,extension = True ,file_type = '' ,contains = '' ):
39
+ def get_last_file_name (file_path : str = None , extension : bool = True , file_type : str = '' , contains : str = '' ):
40
40
if file_path == None : file_path = get_downloads_folder ()
41
41
try :
42
42
list_of_files = [str (file ) for file in pathlib .Path (file_path ).iterdir () if not file .is_dir () and str (file ).endswith (file_type ) and contains in str (file )]
@@ -55,7 +55,7 @@ def get_last_file_name(file_path=None,extension=True,file_type='',contains=''):
55
55
except :
56
56
return print ('Any file was found!' )
57
57
58
- def move_the_last_file (old_path , new_path , file_type = '' ,contains = '' ,delete = False ):
58
+ def move_the_last_file (old_path : str , new_path : str , file_type : str = '' , contains : str = '' , delete : bool = False ):
59
59
list_of_files = [str (i ) for i in pathlib .Path (old_path ).iterdir () if not i .is_dir () and str (i ).endswith (file_type ) and contains in str (i )]
60
60
latest_file = max (list_of_files , key = os .path .getctime )
61
61
new_file = latest_file .replace (old_path ,new_path )
@@ -73,33 +73,33 @@ def move_the_last_file(old_path,new_path,file_type='',contains='',delete=False):
73
73
os .remove (new_file )
74
74
os .rename (latest_file , new_file )
75
75
76
- def rename_file (old_path , new_path , delete = False ):
76
+ def rename_file (old_path : str , new_path : str , delete : bool = False ):
77
77
if delete == False :
78
78
try : os .rename (old_path , new_path )
79
79
except : print ('File already exist!' )
80
80
else :
81
81
try : os .rename (old_path , new_path )
82
82
except : os .remove (new_path ); os .rename (old_path , new_path )
83
83
84
- def copy_the_last_file (old_path , new_path , file_type = '' ):
84
+ def copy_the_last_file (old_path : str , new_path : str , file_type : str = '' ):
85
85
list_of_files = [str (i ) for i in pathlib .Path (old_path ).iterdir () if not i .is_dir () and file_type in str (i )]
86
86
lastest_file = max (list_of_files , key = os .path .getctime )
87
87
lastest_file_name = lastest_file [(len (lastest_file ) - lastest_file [::- 1 ].find ('\\ ' )):]
88
88
new_file_name = lastest_file [(len (lastest_file ) - lastest_file [::- 1 ].find ('\\ ' )):]
89
89
new_file = lastest_file .replace (old_path ,new_path ).replace (lastest_file_name ,new_file_name )
90
90
shutil .copy (lastest_file ,new_file )
91
91
92
- def get_subfolders (folder_path ):
92
+ def get_subfolders (folder_path : str ):
93
93
subfolder = [str (i ) for i in pathlib .Path (folder_path ).iterdir () if i .is_dir ()]
94
94
95
95
return subfolder
96
96
97
- def get_files (folder_path , file_type = '' ,contains = '' ):
97
+ def get_files (folder_path : str , file_type : str = '' , contains : str = '' ):
98
98
files = [str (i ) for i in pathlib .Path (folder_path ).iterdir () if not i .is_dir () and file_type in str (i ) and contains in str (i )]
99
99
100
100
return files
101
101
102
- def get_all_files (folder_path , file_type = '' ,contains = '' ,include_path = True ):
102
+ def get_all_files (folder_path : str , file_type : str = '' , contains : str = '' , include_path : bool = True ):
103
103
filelist = []
104
104
105
105
for root , dir , files in os .walk (folder_path ):
@@ -108,30 +108,29 @@ def get_all_files(folder_path,file_type='',contains='',include_path=True):
108
108
filelist .append (os .path .join (root ,file ))
109
109
110
110
elif str (file ).endswith (file_type ) and contains in str (file ):
111
- filelist .append (file )
112
-
111
+ filelist .append (file )
113
112
return filelist
114
113
115
- def is_file_open (file_path ):
114
+ def is_file_open (file_path : str ):
116
115
try :
117
116
with open (file_path , 'r+' ) as f :
118
117
print ('file is closed, proceed to write' )
119
118
except PermissionError :
120
119
print ('file is open, close it and try again' )
121
120
122
- def get_downloads_folder (downloads_folder_name = 'Downloads' ):
121
+ def get_downloads_folder (downloads_folder_name : str = 'Downloads' ):
123
122
downloads_folder = os .path .join (os .path .expanduser ('~' ), downloads_folder_name )
124
123
return downloads_folder
125
124
126
- def detect_file_download (download_path = None ,tries = 50 ,wait = 1 ):
125
+ def detect_file_download (download_path : str = None , tries : int = 50 , wait : float = 1 ):
127
126
if download_path is None : download_path = get_downloads_folder ()
128
127
tries_count = 0
129
128
while True and tries_count <= tries :
130
129
time .sleep (wait )
131
130
if len (get_files (download_path )) > 0 : break
132
131
else : tries_count += 1
133
132
134
- def detect_file_download_with_criteria (folder_path : str = None ,file_type = '' ,contains = '' ,tries = 50 , wait = 5 ):
133
+ def detect_file_download_with_criteria (folder_path : str = None , file_type : str = '' , contains : str = '' , tries : int = 50 , wait : float = 5 ):
135
134
if folder_path is None : folder_path = get_downloads_folder ()
136
135
137
136
def is_file_downloaded ():
@@ -147,7 +146,7 @@ def is_file_downloaded():
147
146
print ('File was not downloaded!' )
148
147
return False
149
148
150
- def adding_prefix_to_files (file_path : str = None , file_type : str = '' , contains : str = '' ,prefix : str = '' ):
149
+ def adding_prefix_to_files (file_path : str = None , file_type : str = '' , contains : str = '' , prefix : str = '' ):
151
150
if file_path is None : file_path = get_downloads_folder ()
152
151
153
152
list_of_files = [str (i ) for i in pathlib .Path (file_path ).iterdir () if not i .is_dir () and i .endswith (file_type ) and contains in str (i )]
@@ -158,7 +157,7 @@ def adding_prefix_to_files(file_path: str=None, file_type: str='', contains: str
158
157
os .rename (file ,file_new )
159
158
print ('Done!' )
160
159
161
- def remove_files_by_date (folder_path : str , cutoff_date , comparison , include_subs : bool = False ):
160
+ def remove_files_by_date (folder_path : str , cutoff_date : datetime . datetime , comparison : str , include_subs : bool = False ):
162
161
for root , directories , files in os .walk (folder_path ):
163
162
if not include_subs :
164
163
directories .clear ()
@@ -190,7 +189,7 @@ def get_access_date(file_path: str):
190
189
access_date = datetime .datetime .fromtimestamp (timestamp )
191
190
return access_date
192
191
193
- def get_file_size (file_path : str ,size : str = 'default' ):
192
+ def get_file_size (file_path : str , size : str = 'default' ):
194
193
if size == 'default' : file_size = os .path .getsize (file_path )
195
194
elif size == 'kb' : file_size = os .path .getsize (file_path ) / 1024
196
195
elif size == 'mb' : file_size = os .path .getsize (file_path ) / (1024 * 1024 )
@@ -226,16 +225,4 @@ def check_file_type(file: str, file_types: tuple[str]):
226
225
"""
227
226
Check if the file is of a certain type.
228
227
"""
229
- return file .lower ().endswith (file_types )
230
-
231
- def get_local_sharepoint_path (sharepoint_site , raw_adress ):
232
- sharepoint_local_folder = os .path .expanduser ('~/MFP Michelin' )
233
- if raw_adress .startswith ('/' ):
234
- raw_adress = raw_adress [1 :]
235
- for folder in os .listdir (sharepoint_local_folder ):
236
- site = folder .split (' - ' )[0 ]
237
- if sharepoint_site == site or sharepoint_site == folder :
238
- sharepoint_site_folder = folder
239
- break
240
- return os .path .join (sharepoint_local_folder , sharepoint_site_folder , raw_adress )
241
-
228
+ return file .lower ().endswith (file_types )
0 commit comments