Python: printing a file to stdout

后端 未结 8 1970
眼角桃花
眼角桃花 2020-12-08 03:55

I\'ve searched and I can only find questions about the other way around: writing stdin to a file :)

Is there a quick and easy way to dump the contents of a file to s

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 04:05

    To improve on @bgporter's answer, with Python-3 you will probably want to operate on bytes instead of needlessly converting things to utf-8:

    >>> import shutil
    >>> import sys
    >>> with open("test.txt", "rb") as f:
    ...    shutil.copyfileobj(f, sys.stdout.buffer)
    

提交回复
热议问题