-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackuper.py
51 lines (45 loc) · 1.5 KB
/
backuper.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
from pathlib import *
import shutil # Нужен для копирования файлов
dirs = [{
'main_dir': Path('I:\\САЙТЫ ДЛЯ ФОТОГРАФОВ'),
'backup_dir': Path('H:\\Б - САЙТЫ ДЛЯ ФОТОГРАФОВ')},
{
'main_dir': Path('E:\\PYTHON'),
'backup_dir': Path('H:\\Б - Python')},
]
count_files, count_dirs = 0, 0
# Собираем путь
def path_assemble(path):
new_path = backup_dir
parts = list(path.parts[len_main_dir:]) # отсекаем часть пути слева
for part in parts:
new_path = new_path / part
return new_path
# Рекурсивно погружаемся в каталоги
def dipping(path):
global count_files
global count_dirs
for path in path.iterdir():
if path.is_dir():
copy_dir = path_assemble(path)
if not copy_dir.exists():
copy_dir.mkdir()
print(f'Создана папка "{path}"')
count_dirs += 1
dipping(path)
else:
copy_file = path_assemble(path)
if not copy_file.exists() or path.stat().st_mtime > copy_file.stat().st_mtime:
try:
shutil.copy(path, copy_file)
except PermissionError:
print(f'Ошибка доступа! "{path}"')
print(f'Скопирован файл "{path}"')
count_files += 1
for item in dirs:
main_dir = item['main_dir']
backup_dir = item['backup_dir']
len_main_dir = len(main_dir.parts)
dipping(main_dir)
print('====================================')
print(f'Создано {count_dirs} папок, скопировано {count_files} файлов')