|
1
|
|
|
import os |
|
2
|
|
|
import shutil |
|
3
|
|
|
from pathlib import Path |
|
4
|
|
|
|
|
5
|
|
|
from tqdm import tqdm |
|
6
|
|
|
|
|
7
|
|
|
from dirutility.walk import DirPaths |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
View Code Duplication |
class FlattenTree: |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def __init__(self, directory, target='root'): |
|
13
|
|
|
""" |
|
14
|
|
|
Loops through parent folders in a root directory and moves child files out of sub folders and into parent folder |
|
15
|
|
|
:param root: Starting directory |
|
16
|
|
|
:param target: Target destination for files. Default is 'directory' which will move files into corresponding |
|
17
|
|
|
directory under parent root. If set to 'root' then files will be moved into root folder |
|
18
|
|
|
""" |
|
19
|
|
|
self.directory = directory |
|
20
|
|
|
self.target = target |
|
21
|
|
|
self.root_paths = [Path(self.directory + os.sep + dirs) for dirs in os.listdir(self.directory)] |
|
22
|
|
|
self.flatten() |
|
23
|
|
|
|
|
24
|
|
|
@staticmethod |
|
25
|
|
|
def remove_path_dirname(path): |
|
26
|
|
|
""" |
|
27
|
|
|
Removes file paths directory if the directory is empty |
|
28
|
|
|
:param path: Filepath |
|
29
|
|
|
""" |
|
30
|
|
|
dir_to_remove = os.path.dirname(path) |
|
31
|
|
|
if not os.listdir(dir_to_remove): |
|
32
|
|
|
shutil.rmtree(dir_to_remove) |
|
33
|
|
|
else: |
|
34
|
|
|
if len(os.listdir(dir_to_remove)) == 1: |
|
35
|
|
|
if os.listdir(dir_to_remove)[0].startswith('.'): |
|
36
|
|
|
shutil.rmtree(dir_to_remove) |
|
37
|
|
|
|
|
38
|
|
|
def flatten(self): |
|
39
|
|
|
for dirs in tqdm(self.root_paths, desc='Flattening file tree', total=len(self.root_paths)): |
|
40
|
|
|
files = DirPaths(dirs) |
|
41
|
|
|
for f in files: |
|
42
|
|
|
if self.target == 'directory': |
|
43
|
|
|
shutil.move(f, dirs) |
|
44
|
|
|
self.remove_path_dirname(f) |
|
45
|
|
|
|
|
46
|
|
|
elif self.target == 'root': |
|
47
|
|
|
shutil.move(f, self.directory) |
|
48
|
|
|
self.remove_path_dirname(f) |
|
49
|
|
|
self.remove_path_dirname(os.path.dirname(f)) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
class CreateTree: |
|
|
|
|
|
|
53
|
|
|
""" |
|
54
|
|
|
Create file tree from directory of indexed files |
|
55
|
|
|
:param root: Starting directory |
|
56
|
|
|
:param delimiter: Character separating filename attributes |
|
57
|
|
|
:param prefix: Prefix string to be concatenated with lowest level folder |
|
58
|
|
|
:param suffix: Suffix string to be concatenated with lowest level folder |
|
59
|
|
|
""" |
|
60
|
|
|
|
|
61
|
|
|
def __init__(self, root, delimiter="_", prefix=None, suffix=None): |
|
62
|
|
|
self.files = DirPaths(root).files() |
|
63
|
|
|
self.delimiter = delimiter |
|
64
|
|
|
self.prefix = prefix |
|
65
|
|
|
self.suffix = suffix |
|
66
|
|
|
self.create() |
|
67
|
|
|
|
|
68
|
|
|
def rename(self, path): |
|
69
|
|
|
split = os.path.basename(path).split(self.delimiter)[:-1] |
|
70
|
|
|
if self.prefix: |
|
71
|
|
|
split[-1] = self.prefix + self.delimiter + split[-1] |
|
72
|
|
|
if self.suffix: |
|
73
|
|
|
split[-1] = split[-1] + self.delimiter + self.suffix |
|
74
|
|
|
return split |
|
75
|
|
|
|
|
76
|
|
|
def create(self): |
|
77
|
|
|
# Generate lists of files and folders in root directory |
|
78
|
|
|
for path in self.files: |
|
79
|
|
|
child_dirs = self.rename(path) |
|
80
|
|
|
directory = os.path.dirname(path) |
|
81
|
|
|
for each in child_dirs: |
|
82
|
|
|
directory = os.path.join(directory, each) |
|
83
|
|
|
if not os.path.isdir(directory): |
|
84
|
|
|
os.mkdir(directory) |
|
85
|
|
|
shutil.move(path, directory) |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
View Code Duplication |
def move_files_to_folders(directory): |
|
|
|
|
|
|
89
|
|
|
""" |
|
90
|
|
|
Loops through a parent directory and nests files within folders that already exists and have matching prefixs |
|
91
|
|
|
:param directory: Starting directory |
|
92
|
|
|
""" |
|
93
|
|
|
# Generate lists of files and folders in root directory |
|
94
|
|
|
files, folders, files_to_move = [], [], [] |
|
95
|
|
|
for path in os.listdir(directory): |
|
96
|
|
|
if os.path.isdir(path): |
|
97
|
|
|
folders.append(path) |
|
98
|
|
|
elif os.path.isfile(path): |
|
99
|
|
|
files.append(path) |
|
100
|
|
|
|
|
101
|
|
|
# Loop through folders in root directory |
|
102
|
|
|
for path in folders: |
|
103
|
|
|
# Generate list of files with same prefix as current folder |
|
104
|
|
|
child_files = [f for f in files if f.startswith(path)] |
|
105
|
|
|
files_to_move.append((path, child_files)) |
|
106
|
|
|
|
|
107
|
|
|
# Move child files into parent folder |
|
108
|
|
|
for each in files_to_move: |
|
109
|
|
|
folder, files = each |
|
110
|
|
|
for f in files: |
|
111
|
|
|
shutil.move(f, folder) |
|
112
|
|
|
|