Re: problem with read() write()

2009-11-01 Thread Steven D'Aprano
On Sun, 01 Nov 2009 10:44:45 +0100, Alf P. Steinbach wrote: > Could you post (copy and paste) the code, and description of results? Using Python 2.6 under Linux (Fedora 7): >>> f = open('garbage', 'r') # prove the file doesn't exist Traceback (most recent call last): File "", line 1, in IOEr

Re: problem with read() write()

2009-11-01 Thread Matt Nordhoff
Gertjan Klein wrote: > Alf P. Steinbach wrote: > >> 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. > > It does read beyond the end of file. This is perhaps the way the > underlying C library works, but it

Re: problem with read() write()

2009-11-01 Thread Gertjan Klein
Alf P. Steinbach wrote: >* Gertjan Klein: >> I reproduced (with Python 2.5.2 on WinXP) the code the OP wrote after >> creating an empty (0-byte) test file; after the write() the read() >> returns random garbage. I can't imagine why anyone would want that >> behaviour. The file grew to be 4099 byte

Re: problem with read() write()

2009-11-01 Thread Alf P. Steinbach
* Gertjan Klein: Alf P. Steinbach wrote: 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. It does read beyond the end of file. This is perhaps the way the underlying C library works, but it looks like an "unexp

Re: problem with read() write()

2009-11-01 Thread Gertjan Klein
Alf P. Steinbach wrote: >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. It does read beyond the end of file. This is perhaps the way the underlying C library works, but it looks like an "unexpected feature" (re

Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 3:11 pm, Terry Reedy wrote: Great, thanks. > Zeynel wrote: > > On Oct 31, 10:40 am, "Alf P. Steinbach" 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

Re: problem with read() write()

2009-10-31 Thread Terry Reedy
Zeynel wrote: On Oct 31, 10:40 am, "Alf P. Steinbach" 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

Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 10:40 am, "Alf P. Steinbach" 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

Re: problem with read() write()

2009-10-31 Thread Dave Angel
Zeynel wrote: On Oct 31, 9:55 am, "Alf P. Steinbach" wrote: * Zeynel: On Oct 31, 9:23 am, "Alf P. Steinbach" 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 f

Re: problem with read() write()

2009-10-31 Thread Alf P. Steinbach
* Zeynel: On Oct 31, 9:55 am, "Alf P. Steinbach" wrote: * Zeynel: On Oct 31, 9:23 am, "Alf P. Steinbach" 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

Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 9:55 am, "Alf P. Steinbach" wrote: > * Zeynel: > > > > > > > On Oct 31, 9:23 am, "Alf P. Steinbach" 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 >

Re: problem with read() write()

2009-10-31 Thread Alf P. Steinbach
* Zeynel: On Oct 31, 9:23 am, "Alf P. Steinbach" 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 r

Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 9:23 am, "Alf P. Steinbach" 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("h

Re: problem with read() write()

2009-10-31 Thread Alf P. Steinbach
* 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