numpy loadtxt with empty files

Posted on 2011-10-10
Last Modified: 2012-05-12

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.


Question by:Peiris
    LVL 16

    Expert Comment

    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
    LVL 16

    Accepted Solution

    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

        data=np.loadtxt("File Name")
        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:

        data = p.loadtxt("File Name")
        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
    LVL 16

    Expert Comment

    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


    Author Closing Comment

    This solve my problem

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
    Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now