def sed_i(files, expr, replace_exp, only_first_occurrence=False):
"""
Massively search/replace matching lines in files.
Similar to:
sed -i "s/expr/replace_expr/g" files...
:type files: enumerate or list
:param files: file names generator
:type expr: str or pattern
:type replace_exp: str
:param only_first_occurrence: replace only first occurrence per line
"""
r = _compiled_re(expr)
for f in files:
with open(f, 'r') as source:
tmp_f = f + '.pygrep.tmp'
with open(tmp_f, 'w') as dest:
sed(source, r, replace_exp, dest, only_first_occurrence)
shutil.copymode(f, tmp_f)
ori_f = f + '.pygrep.ori'
os.rename(f, ori_f)
os.rename(tmp_f, f)
os.remove(ori_f)
评论列表
文章目录