python file operation

和c语言一样,不能使用'rw'方式打开一个文件并进行读写,正确的方式是‘r+’,详见http://mail.python.org/pipermail/python-bugs-list/2001-May/005216.html


Bugs item #420771, was updated on 2001-05-02 10:11
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=420771&group_id=5470

Category: Python Library
Group: Feature Request
Status: Open
Resolution: None
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Nobody/Anonymous (nobody)
Summary: 'rw' option to open: is it a bug?

Initial Comment:
if you open a file with 'rw'

f = open('foo.bar','rw')
ie you will be able to read, but not write

Python 2.1 (#3, Apr 19 2001, 11:45:56) [C] on sunos5
Type "copyright", "credits" or "license" for more
information.
>>> f = open("foo.bar",'rw')
>>> f.read()
'HelloWorld\n'
>>> f.write("NotWhatIExpect")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 9] Bad file number
>>>

However the rw does not seem to be a valid option, yet
no error is produced upon opening the file, rather
when the operation is attempted.

It seems that what is needed if you want
'rw' is 'r+'.

>>> f = open("foo.bar",'r+')
>>> f.read()
'HelloWorld\n'
>>> f.write("ThisIsWhatIExpect")
>>> f.close()

Could therefore 'rw' signal an error? Or do the
same as 'r+'?





----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2001-05-02 10:57

Message:
Logged In: YES
user_id=31435

Python doesn't look at the mode argument: it passes it on
as-is to the platform C fopen() function. This allows
people to use whatever non-standard modes their platform
supports. An error is reported if and only if the platform
fopen() sets errno across the call.

Mode "rw" isn't defined by C ("r+" and "w+" are), so
whether a platform accepts it is up to the platform; and,
if it does accept it, what it means is also up to the
platform. So that "rw" was accepted when you tried it
means that sunos5's fopen() accepts an "rw" mode. You'll
have to consult Sun's docs for whether it's working as they
intended it to work.

Could be useful if Python warned about non-standard modes.

----------------------------------------------------------------------

You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=420771&group_id=5470


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值