티스토리 뷰

simple python program which replace words with new wrods.

replace_words.py [path] [org_word] [new_word]

path 에 있는 htm/html 들을 찾아서, 그 file 의 org_word 를 전부 new_word 로 바꿔서 그 결과를 새로운 file에 저장하는 프로그램.



#/usr/bin/env python
# -*- coding: mbcs -*-

"""
This simple example is used for the line-by-line tutorial
that comes with pygame. It is based on a 'popular' web banner.
Note there are comments here, but for the full explanation,
follow along in the tutorial.
"""

import os, sys
import string
import re # regular expression
import codecs

import random

        
def replaceWords(inf, outf, org, new) :
       
    rf = codecs.open(inf, "r", "mbcs")               
    wf = codecs.open(outf, "w", "mbcs")
    
    #regular expressions
    reTextorged = re.compile(org, re.M)
    reEmptyLine = re.compile('^\s*$', re.M)
    
    for rawLine in rf:
        textIdx = rawLine.find(org)
        if textIdx is not -1 :
            # get the from-time and to-time
            wf.write(rawLine[:textIdx]+new+rawLine[textIdx+len(org):])
        else:
            wf.write(rawLine)
    
    rf.close()
    wf.close()
    
    return
    


#////////////////////////////////////////////////////////////
def main(argv=None):
    '''
        replace all the 'org' words of the *.htm file in the specific 
        path with the 'new'
    '''
    if argv is None:
        argv = sys.argv
        argc = len(argv)
    
    if argc < 4 :
        print("Usage: run.py {update_file} {from_text} {to_text}")
        sys.exit()
    
    inputPath = argv[1]
    org = argv[2]
    new = argv[3]
    random.seed();  # current system time is used as a seed
    if os.path.exists(inputPath) is True :
        for f in os.listdir(inputPath):  # 'f' is filename
            name, ext = os.path.splitext(f)
            if ext.lower() == '.htm' or ext.lower() == '.html':
                outf = name + str(random.randint(1,50)) + ext.lower()
                replaceWords(f, outf, org, new)


    



if __name__ == '__main__' :
    main()

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/06   »
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
글 보관함