Skip to content

Instantly share code, notes, and snippets.

@anmolj7
Created December 2, 2019 18:51
Show Gist options
  • Save anmolj7/2ef77630c43ae80cce87f3150e1e8642 to your computer and use it in GitHub Desktop.
Save anmolj7/2ef77630c43ae80cce87f3150e1e8642 to your computer and use it in GitHub Desktop.
'''
Write a Python script to search for text in a file and replace it, for example, there is a text file that reads Hello World, change it to Hello name. The intended output is the link to the gist.
'''
fName = input("Enter the file's name: ")
with open(fName) as f:
data = f.read()
repl = input("Enter the text to be replaced: ")
word = input(f"Replace {repl} to: ")
data = data.replace(repl, word)
with open(fName, "w+") as file:
file.write(data)
print("Done...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment