#!/usr/bin/python import os, sys, string from glob import glob exts = ['html', 'htm'] old = ['utf-8', 'UTF-8'] new = ['windows-1251', 'WINDOWS-1251'] DEBUG = 1 def indir(curdir): global space dir = glob('*') for file in dir: ext = os.path.splitext(file)[1][1:] if os.path.isdir(file): if DEBUG: print '%s---> %s' % (' ' * space, file) os.chdir(file) space += 1 indir(file) space -= 1 os.chdir('..') if DEBUG: print '%s<--- %s' % (' ' * space, file) elif exts.__contains__(ext): try: os.system("iconv -f utf8 -t cp1251 %s -o %s.cp1251 " % (file, file)); os.system("rm %s; mv %s.cp1251 %s " % (file, file, file)); text = orig = open(file, 'r').read() i = 0 for word in old: text = text.replace(old[i], new[i]) i += 1 if orig != text: open(file, 'w').write(text) if DEBUG: print '%s+ %s...' % \ (' ' * space, file) else: if DEBUG: print '%s= %s...' % \ (' ' * space, file) except: print 'I/O ERROR while %s processing' % file def Usage(): print 'Usage: convert_charset.py startdir' print space = 0 if len(sys.argv) != 2: Usage() else: startdir = sys.argv[1] if DEBUG: print 'Start with %s' % startdir os.chdir(startdir) indir(startdir) #for file in os.listdir('.'): # file=file.replace("'","\'") # os.system("iconv -f utf8 -t cp1251 \"%s\" -o ./cp1251/\"%s\" " % (file, file)); # os.system("echo 'Edit: %s'" % file); ## print file