def rmsection(filename, pattern):
pattern_compiled = re.compile(pattern)
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
with open(filename) as src_file:
for line in src_file:
(sline, nsub) = pattern_compiled.subn('', line)
tmp_file.write(sline)
if nsub > 0:
next(src_file)
shutil.copystat(filename, tmp_file.name)
shutil.move(tmp_file.name, filename)
# See https://github.com/rtfd/readthedocs.org/issues/1139
评论列表
文章目录