Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- custom filter
- java tip
- JBoss Seam
- JPA
- COC
- CEP
- Hudson
- drools
- @SqlResultSetMapping
- zabbix
- SVN
- querydsl
- GEventEvaluator
- jquery serialize
- jstl
- gwt-ext
- MySQL
- spring jpa
- jenkins
- rember me
- Spring
- Drools Fusion
- bootstrap jquery datepicker
- gwt
- spring security
- ibatis
- jquery
- spring transaction
- guvnor
- maven
Archives
- Today
- Total
봉 블로그
Python replace in file 본문
아래는 파일내 특정문자열을 변경하는 프로그램이다.
단 10분만에 작성한 프로그램인데, 이걸 자바로 하려면 꽤 번거롭다. file inputstream, outputstream 작성해야 하고.. file 목록 뽑아야 하고 등등..
이런 작업은 python의 생산성이 월등히 높다. java와 python 을 같이 사용한다면 매우좋을듯..
from os import listdir from os.path import isfile, join import codecs def replaceInFile(file_path, old, newstr): f = codecs.open(file_path, 'r', encoding='utf8') read_file = f.read() f.close() new_file = codecs.open(file_path,'w', encoding='utf8') for line in read_file.split("\n"): new_file.write(line.replace(old, newstr)) new_file.write("\n") #close file new_file.close() mypath = "D:/was/apache-tomcat-7.0.47-windows-x64/apache-tomcat-7.0.47/webapps/bong" onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ] for fname in onlyfiles: fpath = join(mypath,fname) print fpath replaceInFile(fpath, "old", "new");