-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrename_dirs.py
More file actions
49 lines (38 loc) · 1.69 KB
/
rename_dirs.py
File metadata and controls
49 lines (38 loc) · 1.69 KB
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
import os
import re
def rename_directories(base_path):
"""
Rename directories under the Java path to use 4-digit zero-padded problem numbers.
For example: "123 Problem Name" -> "0123 Problem Name"
"""
java_path = os.path.join(base_path, "java")
if not os.path.exists(java_path):
print(f"Java directory not found at {java_path}")
return
# List all directories under the Java path
dirs = [d for d in os.listdir(java_path) if os.path.isdir(os.path.join(java_path, d))]
# Regular expression to match directory names with numbers
pattern = re.compile(r'^(\d+)(.*)$')
renamed_count = 0
for dir_name in dirs:
match = pattern.match(dir_name)
if match:
# Extract the number and remainder of the directory name
number = match.group(1)
remainder = match.group(2)
# Create new directory name with 4-digit zero-padded number
new_dir_name = f"{int(number):04d}{remainder}"
if dir_name != new_dir_name:
old_path = os.path.join(java_path, dir_name)
new_path = os.path.join(java_path, new_dir_name)
# Rename the directory
try:
os.rename(old_path, new_path)
print(f"Renamed: {dir_name} -> {new_dir_name}")
renamed_count += 1
except Exception as e:
print(f"Error renaming {dir_name}: {e}")
print(f"\nCompleted renaming {renamed_count} directories.")
if __name__ == "__main__":
base_path = r"d:\Code\neet-code-150-python"
rename_directories(base_path)