Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- from fileinput import FileInput
- from re import findall
- def replacement(file, regex, newphrase):
- try:
- with FileInput(file, inplace=True, backup='.bak') as file_founded:
- for line in file_founded:
- if findall(regex, line):
- print(newphrase, end='\n')
- else:
- print(line, end='')
- except FileNotFoundError as err:
- print(err)
- if __name__ == '__main__':
- replacement('testadeiro.txt', '^homem', 'mulher')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement