Skip to content

Instantly share code, notes, and snippets.

Avatar

ice7mayu ice7mayu

View GitHub Profile
@ice7mayu
ice7mayu / replace_text.py
Last active Jul 24, 2017
replace text in-place within a file
View replace_text.py
import fileinput
def replace_text_in_file_demo():
"""
1. The original file is moved to a backup file
2. The standard output is redirected to the original file within the loop
3. Thus any print statements write back into the original file
"""
for line in fileinput.input('foo.txt', inplace=True, backup='.bak'):
You can’t perform that action at this time.