Search and replace text in a file #

#!/usr/bin/env python3
import fileinput

with fileinput.FileInput(filename, inplace=True, backup='.bak') as f:
    for line in f:
        print(line.replace(text_to_search, replacement_text), end='')

Append to a file #

#!/usr/bin/env python3

with open("test.txt", "a") as f:
    f.write("appended text")
Retrieved from http://hyacinth.byus.net/moniwiki/wiki.php/Python/파일 처리 라인 찾아서 교체, 라인 추가
last modified 2019-08-13 22:37:49