file_path = 'your/file/path'
with open(file_path) as inf, open('outfile', 'w') as outf:
for i in inf:
if i.strip() == 'ENDMDL':
break
else:
outf.write(i)
from tempfile import NamedTemporaryFile
from shutil import move
with open("your_file") as f, NamedTemporaryFile("w", dir=".",delete=False) as tmp:
for line in f:
tmp.write(line)
if line.rstrip() == "ENDMDL":
break
move(tmp.name, "your_file")
给你:
使用
tempfile
和shutil.move
替换原始文件:相关问题 更多 >
编程相关推荐