[Numpy-discussion] loadtxt raises an exception on empty file

Nadav Horesh nadavh at visionsense.com
Tue May 25 01:45:35 EDT 2010


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: numpy-discussion-bounces at scipy.org on behalf of Maria Liukis
Sent: Tue 25-May-10 01:14
To: numpy-discussion at scipy.org
Subject: [Numpy-discussion] 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/site-packages/numpy/lib/io.py", line 456, in loadtxt
    raise IOError('End-of-file reached before encountering data.')
IOError: End-of-file 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 at usc.edu



_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion at scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 3377 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100525/8e290818/attachment.bin>


More information about the NumPy-Discussion mailing list