This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.py
48 lines (32 loc) · 1.38 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
import os
os.chdir('/path/to/files/')
# Am I in the correct directory?
# print(os.getcwd())
# print(dir(os))
# Print all the current file names
for f in os.listdir():
# If .DS_Store file is created, ignore it
if f == '.DS_Store':
continue
file_name, file_ext = os.path.splitext(f)
# print(file_name)
# One way to do this
f_title, f_course, f_number = file_name.split('-')
# print('{}-{}-{}{}'.format(f_number, f_course, f_title, file_ext))
# Need to remove whitespace
f_title = f_title.strip()
f_course = f_course.strip()
# f_number = f_number.strip()
# Want to remove the number sign?
# f_number = f_number.strip()[1:]
# One thing I noticed about this output is that if it was sorted by filename
# then the 1 and 10 would be next to each other. How do we fix this? One way we can fix this is to pad
# the numbers. So instead of 1, we'll make it 01. If we had hundreds of files then this would maybe need to be 001.
# We can do this in Python with zfill
f_number = f_number.strip()[1:].zfill(2)
# print('{}-{}-{}{}'.format(f_number, f_course, f_title, file_ext))
# You have the power to reformat in any way you see fit
print('{}-{}{}'.format(f_number, f_title.strip(), file_ext.strip()))
new_name = '{}-{}{}'.format(file_num, file_title, file_ext)
os.rename(fn, new_name)
# print(len(os.listdir()))