[SciPy-user] Reading Empty Arrays

Ryan Krauss ryanlists at gmail.com
Tue Aug 5 07:22:56 EDT 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

else:
    #normal case here

On Tue, Aug 5, 2008 at 2:53 AM, Lorenzo Isella <lorenzo.isella at 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 at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list