-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrename.py
More file actions
32 lines (22 loc) · 756 Bytes
/
Copy pathrename.py
File metadata and controls
32 lines (22 loc) · 756 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
30
31
32
import os
import re
PATH = os.path.abspath(os.path.dirname(__file__))
def rename(n):
if n <= 0:
return
for root, _, files in os.walk(PATH, topdown=True):
for f in files:
if re.match(r"\d+\.\w+", f):
index = f.find(".")
if index == n:
continue
elif n > index:
filename = f[:index].zfill(n) + f[index:]
else:
filename = f[:index][-n:] + f[index:]
filename = os.path.join(root, filename)
if os.path.exists(filename):
os.remove(filename)
os.rename(os.path.join(root, f), filename)
if __name__ == "__main__":
rename(4)