What's the difference between 'r+' and 'a+' when open file in python?
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
python open built-in function: difference between modes a, a+, w, w+, and r+?
我试过用
那么,
添加:
我找到了原因:
我已经读取了文件对象,忘记了查找(0)以将位置设置为开始
python打开文件的方式与在c中几乎相同:
r+ 开放读写。流位于文件的开头。a+ 打开进行读取和附加(在文件末尾写入)。如果文件不存在,则创建该文件。用于读取的初始文件位置位于文件的开头,但输出将附加到文件的结尾(但在某些UNIX系统中,不管当前的查找位置如何)。
一个区别是对于
如果您在
从
r+ : - Open for reading and writing. The stream is positioned at
the
beginning of the file.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.