How to redirect 'print' output to a file using python?

后端 未结 11 1128
既然无缘
既然无缘 2020-11-22 17:05

I want to redirect the print to a .txt file using python. I have a \'for\' loop, which will \'print\' the output for each of my .bam file while I want to redirect ALL these

11条回答
  •  Happy的楠姐
    2020-11-22 17:18

    Changing the value of sys.stdout does change the destination of all calls to print. If you use an alternative way to change the destination of print, you will get the same result.

    Your bug is somewhere else:

    • it could be in the code you removed for your question (where does filename come from for the call to open?)
    • it could also be that you are not waiting for data to be flushed: if you print on a terminal, data is flushed after every new line, but if you print to a file, it's only flushed when the stdout buffer is full (4096 bytes on most systems).

提交回复
热议问题