-
Notifications
You must be signed in to change notification settings - Fork 0
/
organise_by_year_and_rename.py
111 lines (89 loc) · 3.67 KB
/
organise_by_year_and_rename.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
108
109
110
111
"""Script to organise and rename subdirectories within a given directory.
Searches for subdirectories whose names start in the format 'yymmdd', then
organises and renames them.
Author: Jonathan Willitts
"""
import configparser
from datetime import datetime
import logging
import os
import re
def main():
"""Configures logging, reads in config file, then executes organise and
rename function.
"""
# Create logger.
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
log_format = '[%(asctime)s] %(levelname)-8s %(message)s'
log_formatter = logging.Formatter(log_format)
# Create console handler to display logs.
console_handler = logging.StreamHandler()
console_handler.setFormatter(log_formatter)
logger.addHandler(console_handler)
# Read config file.
parser = configparser.ConfigParser()
parser.read('config.ini')
# Create file handler to save logs (if defined).
log_file = parser.get('MISC', 'log_file')
if log_file:
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(log_formatter)
logger.addHandler(file_handler)
logger.info('Logging to log file: %s', log_file)
else:
logger.info('No log file specified.')
# Rename and organise directories.
root_dir = parser.get('MISC', 'root_directory')
organise_by_year_and_rename(root_dir)
def organise_by_year_and_rename(root_dir):
"""Organises subdirectories found in root folder, and renames them.
Searches for subdirectories whose names start in the format 'yymmdd', and
moves them to a folder corresponding to their year 'YYYY', renaming them
to start 'YYYY-mm-dd'.
e.g. would organise and rename a directory in the root directory named:
'161225 - Christmas Day' to:
'2016/2016-12-25 - Christmas Day'
Args:
root_dir: The root directory whose subdirectories are to be organised
and renamed.
Raises:
StopIteration: If specified root directory not found.
"""
logger = logging.getLogger()
logger.info("\n---\nChecking directories in: '%s'\n---", root_dir)
try:
# Get all subdirectories in the root directory.
_, dir_list, _ = next(os.walk(root_dir))
except StopIteration as exc:
logger.critical("Root directory not found: '%s' - %s",
root_dir, str(exc))
raise
# Define pattern for expected date prefix, e.g. 161225 (for 25/12/2016).
date_prefix_length = 6
date_re_pattern = re.compile(
r"^\d{" + re.escape(str(date_prefix_length)) + r"}"
)
for dir_ in dir_list:
# Get dir name prefix (potential date string).
dir_prefix = dir_[:date_prefix_length]
# Only continue if directory name starts with (6) digits.
if re.match(date_re_pattern, dir_prefix):
try:
old_date = datetime.strptime(dir_prefix, "%y%m%d")
except ValueError as exc:
logger.warning(
"Skipping folder '%s' as it does not start in format "
"'yymmdd'. Exception details: %s", dir_, str(exc)
)
else:
dir_suffix = dir_[date_prefix_length:]
new_dir_name = old_date.strftime("%Y-%m-%d") + dir_suffix
dir_year = old_date.strftime('%Y')
new_path = os.path.join(root_dir, dir_year, new_dir_name)
old_path = os.path.join(root_dir, dir_)
# Move to new path/name, creating missing dirs along the way.
logger.info("Moving '%s' to '%s'", old_path, new_path)
os.renames(old_path, new_path)
if __name__ == '__main__':
main()