Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.37 KB | None | 0 0
  1. import fileinput
  2.  
  3.  
  4. def replace_text_in_file_demo():
  5. """
  6. 1. The original file is moved to a backup file
  7. 2. The standard output is redirected to the original file within the loop
  8. 3. Thus any print statements write back into the original file
  9. """
  10. for line in fileinput.input('foo.txt', inplace=True, backup='.bak'):
  11. print line.replace("foo", "bar"),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement