> For the complete documentation index, see [llms.txt](https://l1nwatch.gitbook.io/python-cookbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://l1nwatch.gitbook.io/python-cookbook/di-5-zhang-wen-jian-yu-io/5.2-da-yin-shu-chu-zhi-wen-jian-zhong.md).

# 5.2 打印输出至文件中

## 问题

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

## 解决方案

```python
with open('d:/work/test.txt', 'wt') as f:
    print('Hello World!', file=f)
```
