Problem with .next() method adding junk characters.

John Machin sjmachin at lexicon.net
Mon Oct 2 00:25:48 EDT 2006


Rainy wrote:
> Hi,
>
> I tried searching for this and did not find this issue. I only looked
> at about dozen hits, I apologize if this is covered somewhere and I
> missed it. Without much further ado, here's the thing (Win, Py2.5):
>
> >>> f = open('test', 'w')
> >>> f.fileno()
> 4
> >>> f.write('1\n')
> >>> f.write('2\n3\n4\n')
> >>> f.next()
>
> Traceback (most recent call last):
>   File "<pyshell#8>", line 1, in <module>
>     f.next()
> IOError: [Errno 9] Bad file descriptor

This *should* complain that the file is not open for reading. What you
see is an accidental error. message. When I tried it, I got no error,
but it printed a few hundred bytes of garbage.
In both your case and mine, it has also written a load of junk to the
file!

> >>> f.close()
> >>> f = open('test')
> >>> f.next()
> '1\n'
> >>> f.next()
> '2\n'
> >>> f.next()
> '3\n'
> >>> f.next()
> '4\n'
> >>> f.next()
> '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
> ...many more lines of junk...'

Junk was written to the file earlier.

>
> I understand that you're not
> supposed to call .next on a file open for writing.

Indeed. However if you mess up, Python is supposed to give you a
meaningful error message and not write gibberish to your file.

Please report it as a bug.

Cheers,
John




More information about the Python-list mailing list