Unable to return the list that is read from a file(.csv)
<p>I have been learning and practicing python and during which 
I found one error in my program, but I'm unable to resolve. I want to return list of that is retrieved from a csv file. I tried the below code and it returns me an error. </p>

<pre><code>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()
</code></pre>

<p>And the error is</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>When you use <code>with open</code> it closes the file when the context ends. Now <code>listOfRows</code> is of the return type of <code>csv.Reader</code>, and so is then <code>fullString</code> (not a list). You are trying to iterate on it, which seems to iterate over a file object, which is already closed.</p>

python csv