with example 2

This commit is contained in:
Mert Gör 🇹🇷 2023-08-22 16:45:22 +03:00
parent 86a40c5d91
commit c88cb6a6a8
No known key found for this signature in database
GPG Key ID: 2100A876D55B39B9
2 changed files with 26 additions and 0 deletions

1
python-temel/test.txt Normal file
View File

@ -0,0 +1 @@
this is a test

View File

@ -0,0 +1,25 @@
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