How to implement a stdin, stdout wrapper?

前端 未结 4 2172
挽巷
挽巷 2021-02-10 10:57

I have an interactive program that runs stdin and stdout. I need to create wrapper that will send X to it\'s stdin, check that it prints Y and then redirects wrapper\'s stdin an

4条回答
  •  迷失自我
    2021-02-10 11:24

    You can overwrite the sys module's stdin and stdout

    import sys
    sys.stdin, sys.stdout = wrapper.stdin, wrapper.stdout
    

    These need to be file objects opened for reading and writing respectively. The original stdin and stdout are found at

    sys.stdin, sys.stdout = sys.__stdin__, sys.__stdout__
    

提交回复
热议问题