python - Try/Except Block causing ValueError -


for coding assignment create file read csv file, offer different attributes analysis on (determined column values. had code working perfectly, after added first try/except block started getting following error:

traceback (most recent call last): file "/users/annerussell/dropbox/infotec 1040/module 8/csval.py", line 49, in row1=next(reader, 'end')[0:] valueerror: i/o operation on closed file.

here link file can test if desired. guessed class assignment, , working on learning python gradschool anyway suggestions appreciated.


import csv print('welcome csv analytics!')  # file name , open file while true:   try:       file_name = input('enter name of file process: ')       open(file_name, "rt") infile:  # select attribute analyzed          reader=csv.reader(infile)         headers=next(reader)[0:]         max=len(headers)   except filenotfounderror:        print('the file entered not found. please' \            + ' enter valid file name, ending in .csv.')       continue    except ioerror:       print('the file selected not opened. please ' \             + 'enter valid file name, ending in .csv.')       continue   except:       print('there error opening or reading file. please ' \             + 'enter valid file name, ending in .csv.')       continue   else:         print ('the attributes available analyse are:')        col in range(1, max):         print(col, headers[col])       while true:         try:           choice=int(input('please select number of attribute analyze '))         except:           print('please enter numeric value selection choose.')           continue         else: # build dictionary requested data           dict1= {}           numrows=-1           row1=[]           largest_value=0           key_of_largest_value=0           while row1 != 'end':             row1=next(reader, 'end')[0:]             if row1 !='end':               numrows += 1               key=row1[0]               value=float(row1[choice])               dict1[key] = value               if value>largest_value:                   largest_value=value                   key_of_largest_value=key           #    print('dictionary entry ( key, value)', key, value)           print('largest ', headers[choice], ' value ', key_of_largest_value, ' ', largest_value)   

in short: after with block ends, file closed. can't read it, reader fail. didn't notice there one-space indent with, replace common indent more clear.

seach python context manager more deep understanding.

suggestion here factor out logic try else block process_file function, , call inside with statement.

with open(file_name, "rt") infile:      # select attribute analyzed      reader=csv.reader(infile)     headers=next(reader)[0:]     max=len(headers)     process_file(reader, headers, max)   # <-- 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -