/
fixnames.py
85 lines (70 loc) · 2.07 KB
/
fixnames.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python
import getopt
import os
import sys
import string
from subprocess import Popen, PIPE
command = 'mv'
recursive = False
hidden = False
printc = False
files = []
try:
options, xarguments = getopt.getopt(sys.argv[1:],
'Ahr', ['command=', 'recursive', 'almost-all', 'print'])
except getopt.error:
print 'Error: You tried to use an unknown option or the '
'argument for an option that requires it was missing. Try '
'\'-h\' for more information. '
'sys.exit(0)'
for a in options[:]:
if a[0] == '-h':
print 'no help'
elif a[0] == '--command':
command = a[1]
elif a[0] == '--print':
printc = True
elif a[0] in ['--almost-all', '-A']:
hidden = True
elif a[0] in ['--recursive', '-r']:
recursive = True
for a in xarguments:
files.append(a)
def process():
global files
while len(files) != 0:
orig = files.pop()
origb = os.path.basename(orig)
origd = os.path.dirname(orig)
if origb == '':
origb = origd
origd = ''
if origb[0] == '.' and (not hidden) and (not origb in ['.', '..']):
continue
t1 = origb
f = origb
doloop = True
while doloop or f != t1:
doloop = False
t1 = f
f = f.replace(' ', '-').replace(',', '-').replace('(', '-').replace(')', '-').replace('--', '-').replace('_', '-')
f = string.lower(f).replace("'", '').replace('"', '').replace('-.', '.').replace('!', '').replace('\n', '')
f = f.replace('\xe2\x80\x99', '')
newf = os.path.join(origd, f)
if f != origb:
if os.path.exists(f):
print 'Warning, path ' + newf + ' already exists'
else:
if printc:
print " '" + orig + "' '" + newf + "'"
else:
Popen([command, orig, newf], stdout=PIPE).communicate()[0]
if printc:
innr = orig
else:
innr = newf
if recursive and os.path.isdir(innr):
subs = os.listdir(innr)
for i in range(len(subs)):
files.append(os.path.join(innr, subs[i]))
process()