python-CSD-kursu/python-temel/with.deyimi.2.py
2023-08-22 16:45:22 +03:00

26 lines
489 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class FileWrapper:
def __init__(self, path):
self.f = open(path, 'w')
def write(self, s):
self.f.write(s)
def close(self):
self.f.close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.f.close()
return False
try:
with FileWrapper('test.txt') as f:
f.write('this is a test')
#....
except:
print('file io error!')
# Bu noktada dosya kapatılmış olacak