内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?

Difference between modes a, a+, w, w+, and r+ in built-in open function?(内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?)

本文介绍了内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python内置的open函数中,具体有什么区别在 waw+a+r+ 模式之间?

In the python built-in open function, what is the exact difference between the modes w, a, w+, a+, and r+?

特别是,文档暗示所有这些都将允许写入文件,并说它打开文件是为了附加"、写入"和更新",但没有定义这些术语的含义.

In particular, the documentation implies that all of these will allow writing to the file, and says that it opens the files for "appending", "writing", and "updating" specifically, but does not define what these terms mean.

推荐答案

打开方式与C标准库函数fopen()完全相同.

The opening modes are exactly the same as those for the C standard library function fopen().

BSD fopen 手册页 将它们定义如下:

The BSD fopen manpage defines them as follows:

 The argument mode points to a string beginning with one of the following
 sequences (Additional characters may follow these sequences.):

 ``r''   Open text file for reading.  The stream is positioned at the
         beginning of the file.

 ``r+''  Open for reading and writing.  The stream is positioned at the
         beginning of the file.

 ``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.

 ``a''   Open for writing.  The file is created if it does not exist.  The
         stream is positioned at the end of the file.  Subsequent writes
         to the file will always end up at the then current end of file,
         irrespective of any intervening fseek(3) or similar.

 ``a+''  Open for reading and writing.  The file is created if it does not
         exist.  The stream is positioned at the end of the file.  Subse-
         quent writes to the file will always end up at the then current
         end of file, irrespective of any intervening fseek(3) or similar.

这篇关于内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?

基础教程推荐