1. Code
  2. Python

Replace words in a file

This article is all about really simple code to replace words in a file. You may have issue replacing certain words in a file like yaml or configuration or key files. and you have to do it repeatedly at your work! to avoid iteration of manual work simple code can help out. Using python it is really simple to write such operation and do it often. Below is code that will replace words.

inputFile = 'credentials'
outputFile = 'credentials1'

with open(inputFile, 'r') as infile:
    x = infile.read().strip()
    for line in x:
        r1 = x.replace('name', 'First Name').replace('city', 'CITY').replace('zipcode', 'ZIPcode')
        with open(outputFile, 'w') as outputfil:
            line1 = outputfil.write(str(r1))


Welcome to Typer

Brief and amiable onboarding is the first thing a new user sees in the theme.
Join Typer
Registration is closed.