Python version of freopen()

后端 未结 5 1543
借酒劲吻你
借酒劲吻你 2020-12-29 10:58

Is there anything in python that can replicate the functionality of freopen() in C or C++? To be precise, I want to replicate the functionality of:

freopen(\         


        
5条回答
  •  没有蜡笔的小新
    2020-12-29 11:30

    If you're working on *nix platform, you can write your own freopen.

    def freopen(f,option,stream):
        import os
        oldf = open(f,option)
        oldfd = oldf.fileno()
        newfd = stream.fileno()
        os.close(newfd)
        os.dup2(oldfd, newfd)
    
    import sys
    freopen("hello","w",sys.stdout)
    
    print "world"
    

提交回复
热议问题