problem with read() write()

Zeynel azeynel1 at gmail.com
Sat Oct 31 09:59:25 EDT 2009


On Oct 31, 9:55 am, "Alf P. Steinbach" <al... at start.no> wrote:
> * Zeynel:
>
>
>
>
>
> > On Oct 31, 9:23 am, "Alf P. Steinbach" <al... at start.no> wrote:
> >> * Zeynel:
>
> >>> Hello,
> >>> I've been studying the official tutorial, so far it's been fun, but
> >>> today I ran into a problem with the write(). So, I open the file pw
> >>> and write "hello" and read:
> >>> f = open("pw", "r+")
> >>> f.write("hello")
> >>> f.read()
> >>> But read() returns a bunch of what looks like meta code:
> >>> "ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
> >>> \xa5\x02\x0b
> >>> \x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 0'QUEUE'\np1\n
> >>> (S'exec' ....
> >>> What am I doing wrong? Thank you.
> >> After the 'write' the current position in the file is after the "hello", so
> >> reading will read further content from there.
>
> >> The following works (disclaimer: I'm utter newbie in Python, and didn't consult
> >> the documentation, and it's the first time I've seen the Python 'open'):
>
> >> f = open("pw", "r+")
> >> f.write( "hello" )
> >> f.seek( 0 )  # Go back to start of file
> >> f.read()
> >> f.close()
>
> >> Cheers & hth.,
>
> >> - Alf
>
> > Thanks, but it didn't work for me. I still get the meta file. Although
> > I see that "hello" is there.
>
> Just a thought: try "w+" instead of "r+".
>
> Because if you do
>
>    print( open.__doc__ )
>
> as I recall it said something about "w" truncating the file?
>
> Cheers & hth.,
>
> - Alf

No :) I still got the same thing.



More information about the Python-list mailing list