replace.py 409 B

12345678910111213141516
  1. import glob
  2. import fileinput
  3. import sys
  4. if len(sys.argv) != 4:
  5. print("Usage: python3 replace.py [files_pattern] [old_string] [new_string]")
  6. sys.exit(1)
  7. pattern = sys.argv[1]
  8. old_string = sys.argv[2]
  9. new_string = sys.argv[3]
  10. for file in glob.glob(pattern):
  11. with fileinput.FileInput(file, inplace=True) as f:
  12. for line in f:
  13. print(line.replace(old_string, new_string), end='')