Peiris
asked on
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For my first solution in order to be cleaner you should either close the file before returning or
rewrite as
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
ASKER
This solve my problem
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