-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathselective_copy.py
98 lines (75 loc) · 3.74 KB
/
selective_copy.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#! /usr/bin/env python3
# selective_copy.py - selectively copies a specific filetype's matched
# files to a destination directory (renaming as needed)
import os
import re
import shutil
from pathlib import Path
def selective_copy(source, destination_path, extension):
"""
Selectively copies files of the extension's type from the source
path to the destination path renaming the files as needed (sequentially)
:param str source: path to directory to search (recursively)
:param str destination_path: path to output directory
:param str extension: desired filetype's extension
"""
directory = os.path.abspath(destination_path)
# if destination doesn't exist, it's created
if not os.path.exists(directory):
os.makedirs(directory)
# walks entire tree from source directory
for root, _, files in os.walk(os.path.abspath(source)):
print(f'\nSearching in {root}')
# iterates over each file
for filename in files:
# if it matches desired extension
if filename.endswith(extension):
filepath = os.path.join(root, filename)
destination_path = directory
# skip if source of file is destination
if os.path.dirname(filepath) == directory:
continue
# if file of this name exists in destination
if os.path.exists(os.path.join(destination_path, filename)):
regex = re.compile(f'(.*?)(_\\d+)?({extension})')
increment = 1
check_name = os.path.join(directory,
f'{regex.search(filename).group(1)}'
f'_{increment}{extension}')
# increment filename until name is unique
while os.path.exists(check_name):
check_name = os.path.join(directory,
f'{regex.search(filename).group(1)}'
f'_{increment}{extension}')
increment += 1
destination_path = check_name
# move file to destination
shutil.copy(filepath, destination_path)
print(
f'\tCopied {os.path.basename(filepath)} to {os.path.abspath(destination_path)}')
def simple_selective_copy(source, destination, extension):
"""
Selectively copies files of the extension's type from the source
path to the destination path renaming the files as needed (sequentially)
:param Path source: path to directory to search (recursively)
:param Path destination: path to output directory
:param str extension: desired filetype's extension
"""
matches = list(source.rglob(pattern=f'*{extension}'))
destination.mkdir(exist_ok=True)
for match in matches:
new_path = destination / match.name
if match.parent.resolve() == destination.resolve():
continue
if new_path.exists():
regex = re.compile(f'(.*?)(_\\d+)?({extension})', re.IGNORECASE)
regex_match = regex.search(new_path.name)
increment = int(regex_match.group(2)) + 1 if regex_match.group(2) else 1
while new_path.exists():
new_path = destination / f'{regex_match.group(1)}_{increment}{new_path.suffix}'
increment += 1
shutil.copy(src=f'{match}', dst=f'{new_path}')
print(f'Copied "{match}" to "{new_path}"')
if __name__ == '__main__':
# selective_copy(source='.', destination_path='./matches/', extension='.txt')
simple_selective_copy(source=Path('.'), destination=Path('./matches/'), extension='.txt')