with example 2
This commit is contained in:
parent
86a40c5d91
commit
c88cb6a6a8
1
python-temel/test.txt
Normal file
1
python-temel/test.txt
Normal file
|
@ -0,0 +1 @@
|
|||
this is a test
|
25
python-temel/with.deyimi.2.py
Normal file
25
python-temel/with.deyimi.2.py
Normal 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
|
Loading…
Reference in New Issue
Block a user