[SciPy-user] Reading Empty Arrays

Ryan Krauss ryanlists@gmail....
Tue Aug 5 06:22:56 CDT 2008

Can you test for the emptiness of the file doing something like this:

myfile = open("file004", 'r')
contents = myfile.readlines()

if not contents:
    #empty case here

    #normal case here

On Tue, Aug 5, 2008 at 2:53 AM, Lorenzo Isella <lorenzo.isella@gmail.com> wrote:
> Dear All,
> I have a code which saves in files called file001, file002, and so on
> a set of data.
> Now, each file contains a record of events which may or may not have occurred.
> This is to say that, e.g., file004 can be empty.
> I normally use pylab to read/write files; in my codes I normally use:
> import scipy as s
> import pylab as p
> The problem appears when I try reading one of these arrays.
> E.g.:
> my_array=p.load("file004")
> since then I get an error message.
> These are my points:
> (1) I do not know beforehand which files will be empty
> (2) Since I generate a lot of them which I then post-process
> automatically, I cannot select by hand which are empty and which are
> not.
> (3) Not saving file004 since it is empty would not be a good idea (I
> read the output files sequentially when postprocessing).
> If reading this file004 generated a zero-length array like the one you
> have if you type:
> f=s.zeros(0)
> Then I would be probably fine, since in my postprocessing, certain
> loops depending on the length of the array would be (rightly) skipped
> if the length of the array is zero.
> How can I do that? Basically, I may be in trouble if I have to
> re-write the code generating the results, whereas it would be ideal
> for me to be able to read empty files as zero-length arrays.
> Many thanks
> Lorenzo Isella
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user

More information about the SciPy-user mailing list