-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename.py
49 lines (35 loc) · 1.56 KB
/
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
import os
import re
from natsort import natsorted, ns
# Prompt the user to enter the directory path
directory = input("Enter the directory path where the files are located: ")
# Validate the directory path
while not os.path.isdir(directory):
print("Invalid directory path. Please try again.")
directory = input("Enter the directory path where the files are located: ")
# Prompt the user to enter the season number
season_number = input("Enter the season number (e.g., 01): ")
# Validate the season number
while not season_number.isdigit():
print("Invalid season number. Please enter a valid number.")
season_number = input("Enter the season number (e.g., 01): ")
# Get a list of all files in the directory
files = os.listdir(directory)
# Sort the files using a natural sort with numeric sorting
files = natsorted(files, alg=ns.REAL)
# Regular expression pattern to match the episode number
pattern = r'(\d+)'
# Loop through each file in the directory
for i, file in enumerate(files, start=1):
# Generate the new episode number
episode_number = str(i).zfill(2)
# Generate the new file name
new_file_name = f'S{season_number.zfill(2)}E{episode_number}'
# Get the file extension
file_extension = os.path.splitext(file)[1]
# Generate the new file path
new_file_path = os.path.join(directory, new_file_name + file_extension)
# Rename the file
os.rename(os.path.join(directory, file), new_file_path)
print(f"Renamed '{file}' to '{new_file_path}'")
print("Renaming complete!")