Problem with .next() method adding junk characters.

Rainy ak at silmarill.org
Sun Oct 1 23:55:38 EDT 2006


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
>>> 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...'

I'm not actually trying to do something particular, I'm making snippets
of example code for all functions in LibRef and I ran into this, and
I'm just curious as to what's happening. I understand that you're not
supposed to call .next on a file open for writing. But I don't know why
and how it does what happened here; besides, I've seen the same thing
happen before when I was doing something else with file
open/write/close, but I don't remember the specifics.




More information about the Python-list mailing list