和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