- 0
- 이니스프리
- 조회 수 543
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import os, fnmatch def folderReplace(find_str, replace_str, filePattern): count = 0 current_file = os.path.basename(__file__) for path, dirs, files in os.walk(os.path.dirname(os.path.abspath(__file__))): for filename in fnmatch. filter (files, filePattern): if filename = = current_file: continue filepath = os.path.join(path, filename) with open (filepath, 'rt' , encoding = 'UTF-8' ) as f: s = f.read() if s.find(find_str) ! = - 1 : print (filepath) s = s.replace(find_str, replace_str) count + = 1 with open (filepath, 'wt' , encoding = 'UTF-8' ) as f: f.write(s) print ( 'total : ' + str (count)) folderReplace( '변경 전 단어' , '변경 후 단어' , '*.변경할 확장자' ) |
replace 횟수도 출력되고, 스크립트 파일명을 굳이 입력할 필요가 없도록
아래 URL에 올렸던 파일을 조금 개량했어요 ^^
https://studyforus.com/tipnknowhow/641299
작성자
댓글 0
권한이 없습니다.