Search and replace text in a file
#
Copy
#!/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
#
Copy
#!/usr/bin/env python3
with
open
(
"
test.txt
"
,
"
a
"
)
as
f:
f.write(
"
appended text
"
)
Retrieved from https://hyacinth.kr:443/moniwiki/wiki.php/Python/파일 처리 라인 찾아서 교체, 라인 추가
last modified 2019-08-13 22:37:49