-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrename.py
52 lines (47 loc) · 2.37 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
49
50
51
52
# -*- coding: utf-8 -*-
import os
def renameFiles(folder='specialCharFix'):
print 'Renaming files of ' + folder + ' and its subfolders...'
for dirpath, _, filenames in os.walk(folder, topdown=True):
print '\nRenaming files from', dirpath, '\n'
if filenames:
for filename in filenames:
replaced_name = filename.replace(",", "-")
replaced_name = replaced_name.replace("~", "-")
replaced_name = replaced_name.replace(";", "-")
replaced_name = replaced_name.replace("#", "-")
replaced_name = replaced_name.replace("*", "-")
replaced_name = replaced_name.replace("–", "-") #this is the long dash
replaced_name = replaced_name.replace("ca.", "ca-")
replaced_name = replaced_name.replace("[", "-")
replaced_name = replaced_name.replace("]", "-")
replaced_name = replaced_name.replace("(", "-")
replaced_name = replaced_name.replace(")", "-")
print filename, "->", replaced_name
os.rename(
dirpath + '/' + filename, dirpath + '/' + replaced_name)
print '\nAll files renamed!'
if __name__ == "__main__":
renameFiles()
def rename(folder='specialCharFix'):
print 'Renaming folders'
for root, dirs, files in os.walk(folder):
for directory in dirs:
replaced_name = directory.replace("–", "-")
replaced_name = replaced_name.replace("–","-")
replaced_name = replaced_name.replace(",", "-")
replaced_name = replaced_name.replace("~", "-")
replaced_name = replaced_name.replace(";", "-")
replaced_name = replaced_name.replace("#", "-")
replaced_name = replaced_name.replace("*", "-")
replaced_name = replaced_name.replace("ca.", "ca-")
replaced_name = replaced_name.replace("[", "-")
replaced_name = replaced_name.replace("]", "-")
replaced_name = replaced_name.replace("(", "-")
replaced_name = replaced_name.replace(")", "-")
print(directory, "->"), replaced_name
os.rename(
root + '/' + directory, root + '/' + replaced_name)
print 'All folders renamed!'
if __name__ == "__main__":
rename()