Eclipse で Javaソースの package名リファクタリングなど、マウスで操作していると
最近のEclipseは重いせいか、鬱陶しい作業だ。しかもマウスの手元が狂ったら泣きたくなる。
そういう時こそ Python でスクリプトを走らせて実行させるべき。。
以下URLのページが参考になった。。
ディレクトリのファイルを再帰で検索する為に、、、
Pythonでフォルダ内のファイルリストを取得する
単語の置換の為に、、、
[file] Pythonを使ってファイル内のテキストを検索し置換える方法は? [text] [python-3.x] | CODE Q&A 問題解決 [日本語]
置換の関数
import os def replace_word(infile, old_word, new_word): if not os.path.isfile(infile): print ("Error on replace_word, not a regular file : " + infile) sys.exit(1) f1 = open(infile, 'r', encoding='utf-8').read() f2 = open(infile, 'w', encoding='utf-8') f2.write(f1.replace(old_word, new_word))
glob 使う場合、、
import glob for file in glob.glob('C:/pleiades/workspace/example/src/**/*.java', recursive=True): print("%s" % file) replace_word(file, "org.xxx.yyy", "jp.co.yyy")
pathlib 使う場合、
from pathlib import Path p = Path("C:/pleiades/workspace/example/src/") for file in p.glob("**/*.java"): print("%s" % file) replace_word(file, "org.xxx.yyy", "jp.co.yyy")