Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Writer:
- def __init__(self, path: str = 'tmp.txt'):
- self.path = path
- def __enter__(self):
- self.file = open(self.path, 'w')
- return self
- def __exit__(self, exc_type, exc_val, exc_tb):
- self.file.close()
- def print(self, text: str):
- self.file.write(text + '\n')
- print(text)
- with Writer('file.txt') as writer:
- ...
- writer.print('first line')
- ...
- writer.print('second line')
- with open('file.txt', 'r') as f:
- print('File: \n' + f.read())
- '''
- first line
- second line
- File:
- first line
- second line
- '''
Advertisement
Add Comment
Please, Sign In to add comment