Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem with read() write()

Reply
Thread Tools

problem with read() write()

 
 
Zeynel
Guest
Posts: n/a
 
      10-31-2009
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\x 00\x00\x00\x00\x00\x00\x00'QUEUE'\np1\n
(S'exec' ....

What am I doing wrong? Thank you.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-31-2009
* 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\x 00\x00\x00\x00\x00\x00\x00'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
 
Reply With Quote
 
 
 
 
Zeynel
Guest
Posts: n/a
 
      10-31-2009
On Oct 31, 9:23*am, "Alf P. Steinbach" <(E-Mail Removed)> 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\x 00\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.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-31-2009
* Zeynel:
> On Oct 31, 9:23 am, "Alf P. Steinbach" <(E-Mail Removed)> 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\x 00\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
 
Reply With Quote
 
Zeynel
Guest
Posts: n/a
 
      10-31-2009
On Oct 31, 9:55*am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * Zeynel:
>
>
>
>
>
> > On Oct 31, 9:23 am, "Alf P. Steinbach" <(E-Mail Removed)> 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\x 00\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.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-31-2009
* Zeynel:
> On Oct 31, 9:55 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>> * Zeynel:
>>
>>
>>
>>
>>
>>> On Oct 31, 9:23 am, "Alf P. Steinbach" <(E-Mail Removed)> 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\x 00\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.


Hm. Now I had to look in the docs because I thought I'd given bad advice.

Doc of 'open' says:

"The mode 'w+' opens and truncates the file to 0 bytes, while 'r+' opens the
file without truncation."

So with 'w+' the only way to get garbage is if 'read' reads beyond the end of
file, or 'open' doesn't conform to the documentation.

Testing with Python 3.1.1 under Windows XP Pro:

<example>
>>> f = open( "zilly", "w+" )
>>> f.write( "garbagegarbagegarbagegarbagegarbagegarbagegarbage garbagegarbage" )

63
>>> f.close()
>>> f = open( "zilly", "r" )
>>> f.read()

'garbagegarbagegarbagegarbagegarbagegarbagegarbage garbagegarbage'
>>> f.close()
>>> f.open( "zilly", "r+" )

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_io.TextIOWrapper' object has no attribute 'open'
>>> open( "zilly", "r+" )

<_io.TextIOWrapper name='zilly' encoding='cp1252'>
>>> f = open( "zilly", "r+" )
>>> f.write( "hello" )

5
>>> f.seek( 0 )

0
>>> f.read()

'hellogegarbagegarbagegarbagegarbagegarbagegarbage garbagegarbage'
>>> f.close()
>>> f = open( "zilly", "w+" )
>>> f.write( "hello" )

5
>>> f.seek( 0 )

0
>>> f.read()

'hello'
>>> f.close()
>>>

</example>

The "w+" works here. Even if I made a typing mistake and apparently left the
file open in the middle there.


Cheers & hth.,

- Alf
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      10-31-2009
Zeynel wrote:
> On Oct 31, 9:55 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>
>> * Zeynel:
>>
>>
>>
>>
>>
>>
>>> On Oct 31, 9:23 am, "Alf P. Steinbach" <(E-Mail Removed)> 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 =pen("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\x 00\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 =pen("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.
>
>

When you ran the program, the file already existed. So you're mixing
the old content and the new. If you don't want that, then don't use r+.

When testing, you should start with the system in a known state. Try
initializing (with a text editor for example) that file before trying
the program.

And if it still gives you problems, show us the data in the file before
running your code, your new code, and the results you get. One thing
that'd be useful is to actually assign the results of the read()
function to a variable and print it.

DaveA

 
Reply With Quote
 
Zeynel
Guest
Posts: n/a
 
      10-31-2009
On Oct 31, 10:40*am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
Thanks! This works. But I need to close the file before read and open
it again with "r", otherwise I get the garbage again. Can you give me
the link where you got this in documentation:

"The mode 'w+' opens and truncates the file to 0 bytes, while 'r+'
opens the
> file without truncation."


Only place i could find it was in this bug report: http://bugs.python.org/issue5061

> * Zeynel:
>
>
>
>
>
> > On Oct 31, 9:55 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> >> * Zeynel:

>
> >>> On Oct 31, 9:23 am, "Alf P. Steinbach" <(E-Mail Removed)> 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\x 00\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.

>
> Hm. Now I had to look in the docs because I thought I'd given bad advice.
>
> Doc of 'open' says:
>
> * *"The mode 'w+' opens and truncates the file to 0 bytes, while 'r+' opens the
> * * file without truncation."
>
> So with 'w+' the only way to get garbage is if 'read' reads beyond the end of
> file, or 'open' doesn't conform to the documentation.
>
> Testing with Python 3.1.1 under Windows XP Pro:
>
> <example>
> *>>> f = open( "zilly", "w+" )
> *>>> f.write( "garbagegarbagegarbagegarbagegarbagegarbagegarbage garbagegarbage" )
> 63
> *>>> f.close()
> *>>> f = open( "zilly", "r" )
> *>>> f.read()
> 'garbagegarbagegarbagegarbagegarbagegarbagegarbage garbagegarbage'
> *>>> f.close()
> *>>> f.open( "zilly", "r+" )
> Traceback (most recent call last):
> * *File "<stdin>", line 1, in <module>
> AttributeError: '_io.TextIOWrapper' object has no attribute 'open'
> *>>> open( "zilly", "r+" )
> <_io.TextIOWrapper name='zilly' encoding='cp1252'>
> *>>> f = open( "zilly", "r+" )
> *>>> f.write( "hello" )
> 5
> *>>> f.seek( 0 )
> 0
> *>>> f.read()
> 'hellogegarbagegarbagegarbagegarbagegarbagegarbage garbagegarbage'
> *>>> f.close()
> *>>> f = open( "zilly", "w+" )
> *>>> f.write( "hello" )
> 5
> *>>> f.seek( 0 )
> 0
> *>>> f.read()
> 'hello'
> *>>> f.close()
> *>>>
> </example>
>
> The "w+" works here. Even if I made a typing mistake and apparently left the
> file open in the middle there.
>
> Cheers & hth.,
>
> - Alf


 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      10-31-2009
Zeynel wrote:
> On Oct 31, 10:40 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> Thanks! This works. But I need to close the file before read and open
> it again with "r", otherwise I get the garbage again. Can you give me
> the link where you got this in documentation:
>
> "The mode 'w+' opens and truncates the file to 0 bytes, while 'r+'
> opens the
>> file without truncation."

>
> Only place i could find it was in this bug report: http://bugs.python.org/issue5061


LibRef / builtin functions /open
The entry is a full page.

Get familiar with this section and the Built-in Types section.

Terry Jan Reedy

 
Reply With Quote
 
Zeynel
Guest
Posts: n/a
 
      10-31-2009
On Oct 31, 3:11*pm, Terry Reedy <(E-Mail Removed)> wrote:

Great, thanks.

> Zeynel wrote:
> > On Oct 31, 10:40 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> > Thanks! This works. But I need to close the file before read and open
> > it again with "r", otherwise I get the garbage again. Can you give me
> > the link where you got this in documentation:

>
> > "The mode 'w+' opens and truncates the file to 0 bytes, while 'r+'
> > opens the
> >> * * file without truncation."

>
> > Only place i could find it was in this bug report:http://bugs.python.org/issue5061

>
> LibRef / builtin functions /open
> The entry is a full page.
>
> Get familiar with this section and the Built-in Types section.
>
> Terry Jan Reedy


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments