[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1613
  • Last Modified:

numpy loadtxt with empty files


I use the following code to read my data files

data=np.loadtxt("File Name")

this works fine as long as the data file is not empty. When the data file is empty it give me an error and exit from the program. the data files are generated by simulations and do idea if data exists or not in given file.

I need to check if the data file has data or not and do not know how to do it with python (numpy). when I say data, it doesn't include comments starts with '#'. Every data file generated has comments, so even with bunch of comments it might not has any data.

thanks
~Peiris


0
Peiris
Asked:
Peiris
  • 3
1 Solution
 
gelonidaCommented:
Two suggestions from somebody who doesn't know whether there is a trick in numpy itself.

1.) create a function, which opens the file, and reads lineby line and closes it when reahing the end of file or when encountering the first data.

# Code is untested I directly typed it into this response
def file_is_empty(filename):
    fin = open(filename)
    for line if fin:
        line = line[:line.find('#')]  # remove '#' comments
        line = line.strip()
        if len(line) != 0:
            return False
    return True
0
 
gelonidaCommented:
Suggestion 2:


ryour code with an empty file and check which exception you get
run it with a non existing file to be sure this is a different exception

if the exceptions are different

try:
    data=np.loadtxt("File Name")
except NAME_OF_EXCEPTION_BEING_RAISED_IF_FILE_IS_EMPTY:
    print "file is empty."
    # do whatever you want to do if the file is empty



if the exceptions where the same for non existing files and for empty files and you would like to distinguish both situations:

try:
    data = p.loadtxt("File Name")
except EXCEPTION_BEING_RAISED_FOR_EMPTY_OR_MISSING_FILE as exc:
    if not os.path.exists('File Name'):
        raise exc
    # perhaps you had to check as well for file is not readable ??
   
    # no do whatever is appropriate in your code
0
 
gelonidaCommented:
For my first solution in order to be cleaner you should either close the file before returning or
rewrite as
def file_is_empty(filename):
    with open(filename)as fin:
        for line if fin:
            line = line[:line.find('#')]  # remove '#' comments
            line = line.strip() #rmv leading/trailing white space
            if len(line) != 0:
                return False
    return True

Open in new window

0
 
PeirisAuthor Commented:
This solve my problem
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now