[Pythonmac-SIG] Frustration gone, for now...

Chris Scott kaffeen@mac.com
Fri, 02 Nov 2001 12:45:24 -0500


Thanks for the help. Actually, I discovered the whole f.seek() thing quite
soon after I sent my message of frustration. Nice to know I'm discovering
bugs, though. And me a total newbie!

Sorry to vent on the list... (head hanging sheepishly)
- Chris

From: David Glasser <me@davidglasser.net>
Date: Thu, 1 Nov 2001 16:38:16 -0500
To: pythonmac-sig@python.org
Subject: Re: [Pythonmac-SIG] Frustration building, please help...

>Well, so far Python hasn't been too hard to start learning, except that it
>doesn't really seem to work. I'm working on a Mac G4 OS9 and can't even seem
>to read, write, or manipulate files. Here's what I get, just trying to do
>some simple stuff right out of the tutorial...

I'm not quite sure what exactly you're trying to do.  However, I'll note
that using open modes like 'r+' in any  language is usually not "some
simple stuff".  I'll try to walk through what you have.

Note that when using 'r+' mode, the file is opened with the current
position at the beginning; both reading and writing advances this current
position.  This means that after you write something, trying to read from
the file gives you what is *after* what you just wrote.  You can find out
your position in the file with f.tell, and set it with f.seek.

>>>> f=open('blah', 'r+')

You open a preexisting file.

>>>> f.write('testing 123')

You change its first eleven bytes, leaving your position after those bytes.

>>>> f.read()
>Traceback (most recent call last):
>  File "<input>", line 1, in ?
>IOError: (0, 'Error')

I'm not sure why this happens.

>>>> f.readline()
>''

You read to the end of the line, which seems to be nothing (i.e., you were
at the end of the line already).

>>>> f.write('testing again')

You change thirteen more bytes.

>>>> f.readline()
>'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
>0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
>0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
>0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
>0\x00\x00\x00\x00\x00\x00\x00\x00widths\x06\x00\x00\x00\x06\xd0\x00\x00\x17\
>xa0\x12m<\x81\x12n\x13X\x12mO\xb8lls\x01\x00\x00\x00Ws\n'

You read the (binary, it seems) file up untill the next newline.


You might want to start with text files instead of binary files, and avoid
'r+' mode unless it is absolutely necessary.

--David Glasser

me@davidglasser.net





From: Jack Jansen <jack@oratrix.nl>
Date: Thu, 01 Nov 2001 22:51:43 +0100
To: pythonmac-sig@python.org
Subject: Re: [Pythonmac-SIG] Frustration building, please help...

To expand on David's excellent explanation of why what you're doing
probably isn't what you intended: you also happened to stumble across
two distinct bugs. But they're bugs that only happen in rare cases, I
wasn't aware of them (i.e. nobody reported them) until very recently.

Recently, David Glasser <me@davidglasser.net> said:
> >>>> f.read()
> >Traceback (most recent call last):
> >  File "<input>", line 1, in ?
> >IOError: (0, 'Error')

You cannot switch from reading to writing without doing a seek() or
rewind(). But: the underlying stdio library (MSL) is sloppy with its
error codes, it should have returned "Illegal operation" here.

> >>>> f.readline()
> >'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
> >0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
> >0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
> >0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
> >0\x00\x00\x00\x00\x00\x00\x00\x00widths\x06\x00\x00\x00\x06\xd0\x00\x00\x17\
> >xa0\x12m<\x81\x12n\x13X\x12mO\xb8lls\x01\x00\x00\x00Ws\n'

Here you're stumbling across a second bug. You should have gotten the
"illegal operation" again, but due to a bug in the I/O library that
lies even under the MSL stdio I/O library, GUSI, you don't. This bug is
being
worked on, so in the next release you should get the (erronuous:-)
"Error 0" again. I have no idea where these bytes come from, but
probably not from your file.

--
Jack Jansen             | ++++ stop the execution of Mumia Abu-Jamal ++++
Jack.Jansen@oratrix.com | ++++ if you agree copy these lines to your sig
++++
www.cwi.nl/~jack        | see http://www.xs4all.nl/~tank/spg-l/sigaction.htm