-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrename_files_sequentially.py
More file actions
29 lines (22 loc) · 923 Bytes
/
rename_files_sequentially.py
File metadata and controls
29 lines (22 loc) · 923 Bytes
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
import os
from mtlogger import logger
def main():
parent_dir = input('Enter the path to the folder containing your files:\n').strip(' "\'')
items = [f for f in os.listdir(parent_dir) if os.path.isfile(os.path.join(parent_dir, f)) and not f.startswith('.')]
items.sort()
total_items = len(items)
num_digits = len(str(total_items))
for index, old_name in enumerate(items, start = 1):
file_extension = os.path.splitext(old_name)[1]
new_name = f'{str(index).zfill(num_digits)}{file_extension}'
old_path = os.path.join(parent_dir, old_name)
new_path = os.path.join(parent_dir, new_name)
os.rename(old_path, new_path)
logger.log(f'Renamed "{old_name}" to "{new_name}".')
logger.log(f'\nFinished processing "{parent_dir}".')
if __name__ == '__main__':
try:
main()
except Exception as ex:
logger.error(f'An unexpected error occurred: {ex}')
input('Press Enter to exit...')