Python version of freopen()

后端 未结 5 1541
借酒劲吻你
借酒劲吻你 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:27

    This should help:

    import sys
    
    def freopen(filename, mode):
        if mode == "r":
            sys.stdin = open(filename, mode)
    
        elif mode == "w":
            sys.stdout = open(filename, mode)
    
    # ---- MAIN ----
    
    freopen("input.txt", "r")
    freopen("output.txt", "w")
    

提交回复
热议问题