-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_or_rename_files.py
107 lines (96 loc) · 4.1 KB
/
delete_or_rename_files.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
99
100
101
102
103
104
105
106
107
# This script makes a list of all file paths in a given
# folder and its subfolders and then either deletes files
# depending on what publication id the file name contains,
# or renames files and folders if their names contain
# a certain component.
# When running any of the scripts that populate table publication,
# the corresponding XML files are created simultaneously.
# But if you first test the script on your test database,
# and then forget to delete the newly created files as you
# run the script again on the production db, you end up with a
# double set of files: half of them have id:s from the test db,
# and half of them have the id:s from the production db
# that they should actually have. The names of all the different
# folders and subfolders don't contain id:s, though, and thus
# they don't change between the runs, so you can't
# delete the unwanted files without opening every subfolder.
# The db data is fine so it would be a pity to start deleting it
# or using a db backup in order to run the populating script again.
# This script simply deletes the unwanted files for you and you're
# good to go again.
# Alternatively, you create files and folders and then later on discover
# that they should have been named differently, e.g. the name of the
# sender of a letter was actually something else than you first thought.
# This sender may have hundreds of folders and files and we need to change
# their names. Just define what component in the file path you need to
# change and then have everything renamed.
from pathlib import Path
import re
FOLDER = "../documents/Part_2/Letters/Received"
DELETE = False
RENAME = True
OLD_NAME_COMPONENT = "Harald"
NEW_NAME_COMPONENT = "Herman"
# create path object for folder from given filepath string,
# save all paths to files found in this folder or subfolders in a list
def create_file_list(source_folder):
path = Path(source_folder)
file_list = []
iterate_through_folders(path, file_list)
return file_list
# iterate through folders recursively and append filepaths to list
def iterate_through_folders(path, file_list):
for content in path.iterdir():
if content.is_dir():
iterate_through_folders(content, file_list)
else:
file_list.append(content)
# publication_id is present in the file name
# delete all files where publication_id refers
# to the test database
def delete_file_depending_on_filename(file):
file_name = file.stem
search_string = re.compile(r"_(\d+)$")
match = re.search(search_string, file_name)
publication_id = match.group(1)
publication_id = int(publication_id)
if publication_id > 11000:
print(file_name)
file.unlink()
# rename files and folders if their names contain
# a certain component
def rename_file_and_folders(file_list):
for file in file_list:
# get all the individual components of the path
path_parts = list(file.parts)
# no need to check these first parts of the path
new_path = Path(*path_parts[:8])
for part in path_parts[8:]:
old_path = new_path / part
if OLD_NAME_COMPONENT in part:
new_path = new_path / part.replace(OLD_NAME_COMPONENT, NEW_NAME_COMPONENT)
print(old_path)
print(new_path)
# check if this path still exists
# the folder may already have been renamed
# by an earlier iteration, since these files
# share the same folders
if old_path.exists():
old_path.rename(new_path)
# in this case, there's nothing to rename
# just keep going towards the end of the path
else:
new_path = new_path / part
def main():
# get file paths for given folder
file_list = create_file_list(FOLDER)
# delete unwanted files
if DELETE is True:
for file in file_list:
delete_file_depending_on_filename(file)
print("Files deleted.")
# rename files and folders
if RENAME is True:
rename_file_and_folders(file_list)
print("Files and folders renamed.")
main()