python内置函数open用什么打开写模式_python打开内置函数:模式a,a +,w,w +和r +之间的区别?...

这篇博客探讨了Python内置函数open中不同写模式的区别,包括'r', 'r+', 'w', 'w+', 'a'和'a+'。'w'模式会截断文件,覆盖原有内容;'a'模式追加内容,不会覆盖;'w+'和'a+'同时支持读写,'w+'会截断文件,而'a+'保持文件原有内容并允许读取。在'a+'模式下,即使进行seek操作,写入仍会在文件末尾。文章通过示例解释了这些模式的行为,并指出'a+'模式下第二次write可能被忽略或引发IOError的情况。" 42492993,1098138,Android拼图与涂鸦实现技巧详解,"['Android开发', '图片处理', 'UI设计', 'Bitmap操作', 'Canvas绘图']
摘要由CSDN通过智能技术生成

在python内置的open函数中,模式w , a , w+ , a+和r+的确切区别是什么?

特别地,文档暗示所有这些都将允许写入文件,并说它打开文件专门用于“追加”,“写入”和“更新”,但未定义这些术语的含义。

#1楼

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

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.

#2楼

这些选项与C标准库中的fopen函数相同:

w截断文件,覆盖已存在的文件

文件a追加,添加到已经存在的任何内容上

w+打开以进行读取和写入,截断文件,但也允许您回读已写入文件的内容

a+打开以进行追加和读取,使您既可以追加到文件,也可以读取其内容

#3楼

我碰巧试图弄清楚为什么要使用模式“ w +”与“ w”。 最后,我只是做了一些测试。 我看不到'w +'模式有什么用,因为在两种情况下,文件都是从头开始被截断的。 但是,有了“ w +”,您可以在写完后通过回头阅读。 如果您尝试使用“ w”进行任何读取,都会引发IOError。 在模式“ w +”下不使用seek进行读取不会产生任何结果,因为文件指针将位于您写入的位置之后。

#4楼

我注意到,我不时需要重新打开Goog​​le,只是为了构想两种模式之间的主要区别是什么。 因此,我认为下一次阅读图会更快。 也许其他人也会发现它也有帮助。

8771661c45c7326a017ad3218006812a.png

#5楼

相同信息,只是表格形式

| r r+ w w+ a a+

------------------|--------------------------

read | + + + +

write | + + + + +

write after seek | + + +

create | + + + +

truncate | + +

position at start | + + + +

position at end | + +

意义在哪里:(为避免任何误解)

读取-允许从文件读取

写-允许写入文件

create-如果尚不存在则创建文件

截断-在打开文件期间将其清空(删除了文件的所有内容)

开始位置-打开文件后,初始位置设置为文件的开始

末尾位置-打开文件后,将初始位置设置为文件末尾

注意: a和a+始终附加在文件末尾-忽略任何seek运动。

顺便说一句。 至少在我的win7 / python2.7上,对于以a+模式打开的新文件而言,有趣的行为是:

write('aa'); seek(0, 0); read(1); write('b') write('aa'); seek(0, 0); read(1); write('b') -第二次write被忽略

write('aa'); seek(0, 0); read(2); write('b') write('aa'); seek(0, 0); read(2); write('b') -第二次write引发IOError

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值