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>The easy way to solve this problem is to return a <em>list</em> from your function. I know you assigned <code>listOfRows = []</code> but this was overwritten when you did <code>listOfRows = csv.reader(myFile)</code>.</p>

<p>So, the easy solution is:</p>

<pre><code>def returnTheRowsInTheFile(fileName):
    READ = 'r'
    try:
        with open(fileName, READ) as myFile:
            listOfRows = csv.reader(myFile)
        return list(listOfRows) # convert to a list
    except FileNotFoundError:
        print('The file ' +  fileName + ' is not found')
    except: 
        print('Something went wrong')
</code></pre>

<p>You should also read <a href="https://www.python.org/dev/peps/pep-0008/" rel="nofollow">pep8</a> which is the style guide for Python; in order to understand how to name your variables and functions.</p>

python csv