Is it possible to have an alias for sys.stdout in python? -
consider sample python code. reads stdin , writes file.
import sys arg1 = sys.argv[1] f = open(arg1,'w') f.write('<html><head><title></title></head><body>') line in sys.stdin: f.write("<p>") f.write(line) f.write("</p>") f.write("</body></html>") f.close() suppose want modify same program write stdout instead. then, i'll have replace each instance of f.write() sys.stdout.write(). tedious. want know if there way specify f alias sys.stdout, f.write() treated sys.stdout.write().
just do
>>> import sys >>> f = sys.stdout >>> f.write('abc') abc now need f = sys.stdout instead of f = open(filename). (and remove f.close())
also, please consider using following syntax files.
with open(filename, 'r') f: # the file automatically gets closed way.
Comments
Post a Comment