summaryrefslogtreecommitdiffstats
path: root/comic-rename.py
blob: 93037de2f9c7d6635bbb5e55dd71351c945d6f48 (plain)
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
#!/usr/bin/python

import os
import re
import string

def cleanup_filename(strname):
	(strfile, strext) = os.path.splitext(strname)
	strext = string.strip(strext)
	if strext.lower() == '.cbr' or strext.lower() == '.cbz':
		pattobj = re.compile("\(+(.*)")
		pattobj2 = re.compile("__")
		pattobj3 = re.compile("_")
		name = re.sub(pattobj, "", strfile)
		name = re.sub(pattobj2, " ", name)
		name = re.sub(pattobj3, " ", name)
		return name.rstrip() + strext.lower()
	else:
		return ''

def rename_file(currdir,f):
	c = cleanup_filename(f)
	if (c != "" and c != f):
		print "%s -> %s\n" % (f,c)
		os.rename (currdir + os.sep + f, currdir + os.sep + c)
	return

def process_dir(currdir):
	for f in os.listdir(currdir):
		if not os.path.isdir(f):
			rename_file(currdir, f)
	return

def main():
	process_dir('.')
	return

if __name__=='__main__':
	main()