23
loading...
This website collects cookies to deliver better user experience
__enter__
and __exit__
and any other methods if needed. with open('test_ch30.txt' , 'w+') as file:
file.write('Blew the lid of ur life')
# the open file has automaticlly been clossed
Opening a file using with
statement ensures that File descriptor are closed automatically.
with
statment, the __eneter__
method ,which is used to with
statment.with
statments ends, the interpreter invokes the __exit__
method .class ContextManager(object):
"""docstring for ContextManager"""
def __init__(self):
pass
def __enter__(self):
print("__enter__()")
# optionaly return an obj
return "an instance"
def __exit__(self , exc_type , exc_val , traceback):
print("__exit__ with " , exc_val if exc_val else ' nope')
with ContextManager() as e:
print("is a " , e)
with open('test_ch30.txt') as input_file, open('test_ch77.txt', 'w') as output_file:
# do something with both files.
# e.g. copy the contents of input_file into output_file
for line in input_file:
output_file.write(line + '\n')
# It has the same effect as nesting context managers:
with open('test_ch30.txt') as input_file:
with open('test_ch77.txt', 'w') as output_file:
for line in input_file:
output_file.write(line + '\n')
class File():
def __init__(self, filename, mode):
self.filename = filename
self.mode = mode
def __enter__(self):
self.open_file = open(self.filename, self.mode)
return self.open_file
def __exit__(self, *args):
self.open_file.close()
for _ in range(10000):
with File('test_ch30.txt', 'w') as f:
f.write('foo')