[issue15068] fileinput requires two EOF when reading stdin
Joey Geralnik
report at bugs.python.org
Fri Jun 15 16:20:34 CEST 2012
Joey Geralnik <jgeralnik at gmail.com> added the comment:
First off, I'm a complete noob looking at the python source code for the first time so forgive me if I've done something wrong.
What if the length of the chunk is checked as well? The following code works fine:
import sys
while True:
chunk = sys.stdin.read(1000)
if not chunk:
break
# process
if len(chunk) < 1000:
break
Something similar could be done in the fileinput class. The patch I've attached checks if the number of bytes read from the file is less than the size of the buffer (which means that the file has ended). If so, the next time the file is to be read it skips to the next file instead.
joey at j-Laptop:~/cpython$ ./python
Python 3.3.0a3+ (default:befd56673c80+, Jun 15 2012, 17:14:12)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import fileinput
[73732 refs]
>>> lines = list(fileinput.input())
foo
bar
^D
[73774 refs]
>>> lines
['foo\n', 'bar\n']
[73780 refs]
----------
keywords: +patch
nosy: +jgeralnik
Added file: http://bugs.python.org/file26018/fileinput.patch
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15068>
_______________________________________
More information about the Python-bugs-list
mailing list