I am wondering if the build-in function
Also, may I know if it is possible to modify the source code of the build-in
You can create a class with a
write method and inside of that method you can
stdout as well as
write to the file.
def __init__(self, f):
self.f = open(f, 'w')
return self # return instance of A which is assign to `f`.
def write(self, text):
sys.stdout.write(text) # print to the shell
self.f.write(text) # write in the file
def __exit__(self, *args):
with A('foo.txt') as f:
print("test0","test1","test4",sep='\n', file=f) #`file = f` calls `write` method