-
Notifications
You must be signed in to change notification settings - Fork 7
/
fix-filenames.py
executable file
·71 lines (60 loc) · 1.83 KB
/
fix-filenames.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python2.4
#
#######################################################################
#
#
# fix-filenames.py
#
# USAGE: ./fix-filenames.py dir1 [dir2 ...]
#
# Fix filenames and directory names, such that any "weird" character
# is converted to an underscore.
#
# A "weird" character is a non-alphanumeric character that is not
# in the following list:
# .-/[](),!_'&+#$~
#
# For each directory given on the command-line, we fix the name of
# *every* filename and directory name therein, recursing on subdirectories.
#
#
# fix-filenames.py,v 1.1 2004/12/15 11:07:14 turian Exp
#
#
#######################################################################
import os,sys,re
if len(sys.argv) < 2:
sys.stderr.write("Incorrect call.\nUSAGE: ./fix-filenames.py dir1 [dir2 ...]\n")
assert(0)
list = []
dir = sys.argv[1:]
badre = re.compile("[^a-z0-9\s\.\-\/\[\]\(\)\,\!\_\'\&\+\#\$\~]", re.I)
def fixdirectory(d):
done = False
while not done:
done = True
for (dirpath, dirnames, filenames) in os.walk(d):
#print (dirpath, dirnames, filenames)
for f in dirnames + filenames:
assert not badre.search(dirpath)
origf = dirpath + "/" + f
if not badre.search(origf): continue
newf = badre.sub("_", origf)
assert os.access(origf, os.W_OK)
assert not os.access(newf, os.F_OK)
sys.stderr.write("Renaming '%s' to '%s'\n" % (origf, newf))
os.rename(origf, newf)
assert not os.access(origf, os.F_OK)
assert os.access(newf, os.F_OK)
# If we've renamed a filename, then once
# we are done with the current dirpath,
# start again at the beginning of the
# while loop.
# This ensures that we don't have stale paths in
# the os.walk list to the old directory name.
if f in dirnames: done = False
if not done: break
while len(dir):
d = dir[0]
dir = dir[1:]
fixdirectory(d)