首页 > 解决方案 > 通过 open() "w" 模式创建文本文件

问题描述

在哪里记录了 open() 中的“w”模式创建了一个新文件?我浏览了所有信息https://docs.python.org/3/library/functions.html#open,但我没有找到任何关于为什么这个“w”会创建一个新文本文件的信息。

标签: python

解决方案


我相信您使用的是 cpython,它在内部使用 C 标准库函数 fopen()。 http://www.manpagez.com/man/3/fopen/

 ``w''   Truncate file to zero length or create text file for writing.
         The stream is positioned at the beginning of the file.

 ``w+''  Open for reading and writing.  The file is created if it does not
         exist, otherwise it is truncated.  The stream is positioned at
         the beginning of the file.

推荐阅读