-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_files_by_year.py
71 lines (57 loc) · 2.02 KB
/
group_files_by_year.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
import logging
import os
import re
import shutil
import sys
from datetime import datetime
def setup_logging():
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s"
)
def validate_args():
if len(sys.argv) != 3 or sys.argv[1] != "--input-dir":
logging.error("Usage: script.py --input-dir <directory>")
sys.exit(1)
input_dir = sys.argv[2]
if not os.path.isdir(input_dir):
logging.error(f"Provided input directory does not exist: {input_dir}")
sys.exit(1)
return input_dir
def find_markdown_files(input_dir):
markdown_files = []
for root, _, files in os.walk(input_dir):
for file in files:
if file.endswith(".md"):
markdown_files.append(os.path.join(root, file))
return markdown_files
def group_files_by_year(markdown_files):
grouped_files = {}
date_pattern = re.compile(r"^(\d{4})-(\d{2})-(\d{2})")
for file in markdown_files:
filename = os.path.basename(file)
match = date_pattern.match(filename)
if match:
year = match.group(1)
if year not in grouped_files:
grouped_files[year] = []
grouped_files[year].append(file)
return grouped_files
def create_directories_and_move_files(grouped_files, input_dir):
for year, files in grouped_files.items():
year_dir = os.path.join(input_dir, year)
if not os.path.exists(year_dir):
os.makedirs(year_dir)
for file in files:
try:
shutil.move(file, year_dir)
logging.info(f"Moved {file} to {year_dir}")
except Exception as e:
logging.error(f"Error moving file {file} to {year_dir}: {e}")
def main():
setup_logging()
input_dir = validate_args()
markdown_files = find_markdown_files(input_dir)
grouped_files = group_files_by_year(markdown_files)
create_directories_and_move_files(grouped_files, input_dir)
if __name__ == "__main__":
main()