-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrename.py
26 lines (25 loc) · 868 Bytes
/
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
import os
#counter=0
# traverse directory, and list directories as dirs and files as files
for root, dirs, files in os.walk(os.getcwd()):
path = root.split(os.sep)
for file in files:
#rename sub-files
if len(path) > 3:
i = 3
new_name = ""
while i < len(path):
new_name += "(" + path[i] + ")"
i += 1
#to prevent hitting the filename + path character limit of the os
if len(new_name)+len(file.lower()) > 180:
new_name += "---"
break
new_name += file
#print (len(path)," ", new_name)
#counter += 1
try:
os.rename(os.path.join(root,file), os.path.join(root,new_name))
except WindowsError:
continue
#print(counter)