How to bypass Windows 'cooking' the I/O? (One more time, please)

Tim Roberts timr at probo.com
Fri Jul 4 07:40:32 CEST 2008


norseman <norseman at hughes.net> wrote:
>
>Problem:  (sos=same old s...)  Microsoft insists the world work it's way 
>even when the Microsoft way was proven wrong decades ago. In this case 
>it's (still) 'cooking' the writes even with 'rwb' and O_RDWR|O_BINARY in 
>(proper respective) use.

No, it doesn't.  Where did you get the idea that 'rwb' is a valid fopen
mode?  It's not.  If you need to read and write an existing file, you want
"r+b".


C:\tmp>python
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('x.bat','r+b')
>>> s = f.read()
>>> s
'sed -e "s/[ \\t]*$//" -e "/^$/d" %1\rhow about that\r\n'
>>> f.seek(-1,2)
>>> f.write('xxx\r\n')
>>> f.close()
>>> f = open('x.bat','rb')
>>> t = f.read()
>>> t
'sed -e "s/[ \\t]*$//" -e "/^$/d" %1\rhow about that\rxxx\r\n'
>>>

>Same python program runs as expected in Linux.  Maybe because that's 
>where it was written?! :)

Perhaps your Linux C runtime library accepts the fopen mode 'rwb', but if
it does, it's a non-standard extension.

>What I seek is the way to slap Microsoft up side the head and make it 
>work correctly.  OK, well, at least in this situation.

It works correctly if you use it correctly.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list