## Facility for check data file(s) def summaryData (fname) : """fname is the name of a data file print numbers of floats and wrong-formated entries in file fname return the two numbers and summation of the floats in the form of a tuple (num, errnum, summation). """ try: datafile = open(fname) except OSError: print("File cannot open:", fname) raise # re-raise original OSError exception accum = 0.0 num = 0 errnum = 0 for line in datafile: slist = line.strip().split() for s in slist: try: x = float(s) accum += x num += 1 except ValueError: errnum += 1 print("In file " + fname + ":") print("Read correct numbers:", num) print("Format error entries:", errnum) return (num, errnum, accum) if __name__ == "__main__": num = 0 errnum = 0 accum = 0.0 while True: fname = input("Next file name (None to quit): ") if fname == "None": print("Read correct numbers:", num) print("Format error entries:", errnum) print("Accumulated value:", accum) break try: n, e, a = summaryData(fname) num += n errnum += e accum += a except OSError: pass