-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenamer.py
More file actions
34 lines (26 loc) · 1.09 KB
/
Copy pathrenamer.py
File metadata and controls
34 lines (26 loc) · 1.09 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
import os
def rename_files(folder_path, prefix):
try:
# Get a list of all files in the folder
files = os.listdir(folder_path)
# Sort files to ensure consistency
files.sort()
for i, filename in enumerate(files):
# Define the file extension (keeps the original extension)
extension = os.path.splitext(filename)[1]
# New name format: Prefix_Number.extension
new_name = f"{prefix}_{i+1}{extension}"
# Paths
old_path = os.path.join(folder_path, filename)
new_path = os.path.join(folder_path, new_name)
# Rename the file
os.rename(old_path, new_path)
print(f"Renamed: {filename} -> {new_name}")
print("\n✅ All files renamed successfully!")
except Exception as e:
print(f"❌ Error: {e}")
# Usage Instructions:
# 1. Change path to your actual folder path
# 2. Change 'Project_Alpha' to your desired name
path = "./test_folder"
rename_files(path, "Project_Alpha")