moriyoshi (owner)

Revisions

gist: 224215 Download_button fork
public
Public Clone URL: git://gist.github.com/224215.git
Embed All Files: show embed
conv_enc.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
#!/usr/bin/python
 
import sys
import os
import os.path
from optparse import OptionParser, make_option
import locale
 
OPTION_LIST = [
    make_option("-f", "--from", type="string", dest="from_enc"),
    make_option("-t", "--to", type="string", dest="to_enc", default=locale.getpreferredencoding()),
    ]
 
USAGE = "usage: %prog [options] path"
 
class Converter(object):
    def __init__(self, to_enc, from_enc):
        self.to_enc = to_enc
        self.from_enc = from_enc
 
    def doit(self, path):
        for i in os.listdir(path):
            src = os.path.join(path, i)
            dest = os.path.join(path, unicode(i, self.from_enc).encode(self.to_enc))
            if os.path.isdir(src):
                self.doit(src)
            os.rename(src, dest)
 
parser = OptionParser(option_list=OPTION_LIST, usage=USAGE)
options, args = parser.parse_args()
 
if options.from_enc is None:
    parser.error('-f must be supplied')
    parser.print_usage()
    sys.exit(255)
 
if len(args) == 0:
    parser.print_usage()
    sys.exit(255)
 
for path in args:
    Converter(options.to_enc, options.from_enc).doit(path)