Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent or dismiss 'empty file' warning in loadtxt

Tags:

python

numpy

My code goes through a number of files reading them into lists with the command:

data = np.loadtxt(myfile, unpack=True)

Some of these files are empty (I can't control that) and when that happens I get this warning printed on screen:

/usr/local/lib/python2.7/dist-packages/numpy/lib/npyio.py:795: UserWarning: loadtxt: Empty input file: "/path_to_file/file.dat"
  warnings.warn('loadtxt: Empty input file: "%s"' % fname)

How can I prevent this warning from showing?

like image 404
Gabriel Avatar asked Oct 03 '13 19:10

Gabriel


1 Answers

You will have to wrap the line with catch_warnings, then call the simplefilter method to suppress those warnings. For example:

import warnings

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    data = np.loadtxt(myfile, unpack=True)

Should do it.

like image 83
MSalmo Avatar answered Sep 17 '22 16:09

MSalmo