loadtxt raises an exception on empty file
Hello everybody, I'm using numpy V1.3.0 and ran into a case when numpy.loadtxt('foo.txt') raised an exception:
import numpy as np np.loadtxt('foo.txt') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sitepackages/numpy/lib/io.py", line 456, in loadtxt raise IOError('Endoffile reached before encountering data.') IOError: Endoffile reached before encountering data.
if provided file 'foo.txt' is empty. Would anybody happen to know if it's a feature or a bug? I would expect it to return an empty array. numpy.fromfile() handles empty text files:
np.fromfile('foo.txt', sep='\t\n ') array([], dtype=float64)
Would anybody suggest a graceful way of handling empty files with numpy.loadtxt() (except for catching an IOError exception)? Many thanks, Masha  liukis@usc.edu
On Mon, May 24, 2010 at 4:14 PM, Maria Liukis
Hello everybody,
I'm using numpy V1.3.0 and ran into a case when numpy.loadtxt('foo.txt') raised an exception:
import numpy as np np.loadtxt('foo.txt') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sitepackages/numpy/lib/io.py", line 456, in loadtxt raise IOError('Endoffile reached before encountering data.') IOError: Endoffile reached before encountering data.
if provided file 'foo.txt' is empty.
Would anybody happen to know if it's a feature or a bug? I would expect it to return an empty array.
Looking at the source for loadtxt line 591 # Read until we find a line with some values, and use # it to estimate the number of columns, N. first_vals = None while not first_vals: first_line = fh.readline() if first_line == '': # EOF reached raise IOError('Endoffile reached before encountering data.') So it looks like it is not a bug although I am not sure why returning an empty array would not be valid. But then what are you going to do with the empty array? Vincent
numpy.fromfile() handles empty text files:
np.fromfile('foo.txt', sep='\t\n ') array([], dtype=float64)
Would anybody suggest a graceful way of handling empty files with numpy.loadtxt() (except for catching an IOError exception)?
Many thanks, Masha  liukis@usc.edu
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
*Vincent Davis 7203013003 * vincent@vincentdavis.net my blog http://vincentdavis.net  LinkedInhttp://www.linkedin.com/in/vincentdavis
You can just catch the exception and decide what to do with it: try: data = np.loadtxt('foo.txt') except IOError: data = 0 # Or something similar Nadav Original Message From: numpydiscussionbounces@scipy.org on behalf of Maria Liukis Sent: Tue 25May10 01:14 To: numpydiscussion@scipy.org Subject: [Numpydiscussion] loadtxt raises an exception on empty file Hello everybody, I'm using numpy V1.3.0 and ran into a case when numpy.loadtxt('foo.txt') raised an exception:
import numpy as np np.loadtxt('foo.txt') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sitepackages/numpy/lib/io.py", line 456, in loadtxt raise IOError('Endoffile reached before encountering data.') IOError: Endoffile reached before encountering data.
if provided file 'foo.txt' is empty. Would anybody happen to know if it's a feature or a bug? I would expect it to return an empty array. numpy.fromfile() handles empty text files:
np.fromfile('foo.txt', sep='\t\n ') array([], dtype=float64)
Would anybody suggest a graceful way of handling empty files with numpy.loadtxt() (except for catching an IOError exception)? Many thanks, Masha  liukis@usc.edu _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
participants (3)

Maria Liukis

Nadav Horesh

Vincent Davis