我一直在學習和練習python,在此期間我在程序中發現了一個錯誤,但我無法解決。我想要返回從csv文件中檢索的列表。我試過下面的代碼,它返回一個錯誤。無法返回從文件中讀取的列表(.csv)
import csv
def returnTheRowsInTheFile(fileName):
READ = 'r'
listOfRows = []
try:
with open(fileName, READ) as myFile:
listOfRows = csv.reader(myFile)
return listOfRows
except FileNotFoundError:
print('The file ' + fileName + ' is not found')
except:
print('Something went wrong')
finally:
#myFile.close()
print()
def main():
fullString = returnTheRowsInTheFile('ABBREVATIONS.CSV')
for eachRow in fullString:
print(eachRow)
return
main()
和錯誤是
Traceback (most recent call last): File "C:\Users\santo\workspace\PyProject\hello\FinalChallenge.py", line 36, in main() File "C:\Users\santo\workspace\PyProject\hello\FinalChallenge.py", line 32, in main for eachRow in fullString: ValueError: I/O operation on closed file.