开发者社区 问答 正文

打印输出至文件中

你想将 print() 函数的输出重定向到一个文件中去。

展开
收起
哦哦喔 2020-04-17 12:22:11 1023 分享 版权
1 条回答
写回答
取消 提交回答
  • 在 print() 函数中指定 file 关键字参数,像下面这样:
    
    with open('d:/work/test.txt', 'wt') as f:
        print('Hello World!', file=f)
    
    2020-04-17 12:22:23
    赞同 展开评论
问答地址: