This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Using .next() on file open in write mode writes junk to file
Type: Stage:
Components: Documentation Versions: Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, rainy, skip.montanaro
Priority: normal Keywords:

Created on 2006-10-02 04:49 by rainy, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg30101 - (view) Author: andrei kulakov (rainy) Date: 2006-10-02 04:49
When you open a file in write mode and use .next(), it 
prints an error and writes many lines of junk to file. 
I tested on windows and python 2.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\x
00\x00\x00\x00\x00\x­
00\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...'

msg30102 - (view) Author: andrei kulakov (rainy) Date: 2006-10-04 01:23
Logged In: YES 
user_id=511010

Python newsgroup folks said that this is normal because when 
switching between write/read modes without doing flush() 
first, result is undefined. There was a suggestion from 
Terry Ready to add this to documentation on file methods:

"When switching from reading or writing (or vice versa), 
call flush(), or 
the result will be undefined."

thx, -andrei
msg30103 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2006-10-07 10:38
Logged In: YES 
user_id=44345

Works for me.  (Nearly current build from SVN.) I find it implausible that your 
explanation of failing to flush the file is the cause of the problem since closing 
the file will cause it to be flushed.  You didn't open the file for "w+" so there's no 
opportunity to switch between writing and reading.

What platform are you using?
msg30104 - (view) Author: andrei kulakov (rainy) Date: 2006-10-07 16:04
Logged In: YES 
user_id=511010

I tried it on win2k sp4 running python 2.5 and right now 
tried it on winXP running python 2.4 - same error. Also, at 
least one user on python newsgroup tried it and got a 
slightly different result: he did not get the error on
 .next() but the junk was written to the file. I paste my 
latest attempt right out of IDLE, without any modification 
(my first paste was also straight from idle). Except that I 
trim the junk characters, because there's more than a 
screen of 'em.

IDLE 1.1.2      
>>> f = open('test', 'w')
>>> f.write('1\n')
>>> f.next()

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in -toplevel-
    f.next()
IOError: [Errno 9] Bad file descriptor
>>> f.close()
>>> f = open('test')
>>> f.next()
'1\n'
>>> f.next()
"\x95\x00\xc8\xe ......."


Please let me know if you need to know anything else...
msg30105 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2007-03-21 00:44
I think the docs should just say switching is undefined and leave it at that.  Calling 'next' on a file open for writing just makes no sense anyway.  And who knows what the OS will do if you try to misuse a file descriptor.

Other option is to raise an exception when 'next' is called on a file opened for writing (TypeError?).  I think that makes more sense.  Unfortunately having to check the mode every time you call 'next' might be considered unacceptable.  Anyone have an opinion?

Either way I will go with one of the two solutions.
msg30106 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2007-04-08 04:30
r54712 has the documentation note.
History
Date User Action Args
2022-04-11 14:56:20adminsetgithub: 44064
2006-10-02 04:49:36rainycreate