in interactive console:
>>> import sys >>> sys.stdout <open file '<stdout>', mode 'w' @ 0xb7810078> >>> sys.stdout.close() >>> sys.stdout # confirming it's closed (...) valueerror: i/o operation on closed file attempting revert:
>>> sys.stdout.open() (...) attributeerror: 'file' object has no attribute 'open' >>> sys.stdout.write('foo') (...) valueerror: i/o operation on closed file i agree it's frivolous question, i'm curious how sys.stdout.close() can reverted in python (without restarting interactive console, of course) , why sys.stdout.open() not make sense.
okay, hope on unix system...
basically sys.stdout variable containing writable object.
so can magic
sys.stdout = open("file", "w") and can write file if stdout.
knowing unix 1 big box of files. unix kind enough give /dev/stdout
so re-open stdout simple
sys.stdout = open("/dev/stdout", "w") job done, have new stdout opened up.
edit
>>> os.fstat(1) posix.stat_result(st_mode=8592, st_ino=7, st_dev=11l, st_nlink=1, st_uid=1000, st_gid=5, st_size=0, st_atime=1374230552, st_mtime=1374230552, st_ctime=1374230434) >>> sys.stdout.close() >>> sys.stdout = open("/dev/stdout", "w") >>> sys.stdout.fileno() 3 >>> os.fstat(3) posix.stat_result(st_mode=8592, st_ino=7, st_dev=11l, st_nlink=1, st_uid=1000, st_gid=5, st_size=0, st_atime=1374230576, st_mtime=1374230576, st_ctime=1374230434) >>> os.fstat(1) posix.stat_result(st_mode=8592, st_ino=7, st_dev=11l, st_nlink=1, st_uid=1000, st_gid=5, st_size=0, st_atime=1374230576, st_mtime=1374230576, st_ctime=1374230434) >>>
Comments
Post a Comment