How to flush output of print function?

后端 未结 13 1613
旧时难觅i
旧时难觅i 2020-11-21 05:15

How do I force Python\'s print function to output to the screen?

This is not a duplicate of Disable output buffering - the linked question is attempting unbuffe

13条回答
  •  醉梦人生
    2020-11-21 05:31

    Here is my version, which provides writelines() and fileno(), too:

    class FlushFile(object):
        def __init__(self, fd):
            self.fd = fd
    
        def write(self, x):
            ret = self.fd.write(x)
            self.fd.flush()
            return ret
    
        def writelines(self, lines):
            ret = self.writelines(lines)
            self.fd.flush()
            return ret
    
        def flush(self):
            return self.fd.flush
    
        def close(self):
            return self.fd.close()
    
        def fileno(self):
            return self.fd.fileno()
    

提交回复
热议问题