forked from Linbreux/wikmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
28 lines (23 loc) · 1.1 KB
/
utils.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
import os
def move_all_files(src_dir: str, dest_dir: str):
"""
Function that moves all the files from a source directory to a destination one.
If a file with the same name is already present in the destination, the source file will be renamed with a
'-copy-XX' suffix.
:param src_dir: source directory
:param dest_dir: destination directory
"""
if not os.path.isdir(dest_dir):
os.mkdir(dest_dir) # make the dir if it doesn't exists
src_files = os.listdir(src_dir)
dest_files = os.listdir(dest_dir)
for file in src_files:
new_file = file
copies_count = 1
while new_file in dest_files: # if the file is already present, append '-copy-XX' to the file name
file_split = file.split('.')
new_file = f"{file_split[0]}-copy-{copies_count}"
if len(file_split) > 1: # if the file has an extension (it's not a directory nor a file without extension)
new_file += f".{file_split[1]}" # add the extension
copies_count += 1
os.rename(f"{src_dir}/{file}", f"{dest_dir}/{new_file}")