<div dir="ltr">Hi everybody,<div><br></div><div>I&#39;m running python 2.6.1 on vista and I&#39;m trying to use the csv module to write to a csv file and get the average of some numbers, but I keep getting the following error:</div>
<div><br></div><div><div>Traceback (most recent call last):</div><div>  File &quot;C:\Python31\MyCSVProjectFinal.py&quot;, line 83, in &lt;module&gt;</div><div>    writer.writerow(headings)</div><div>IOError: [Errno 9] Bad file descriptor</div>
</div><div><br></div><div>line 83 refers to the following code, specifically to the one in capital (in the actual code it&#39;s not in capital by the way):</div><div><br></div><div><div>headings = linesInCSV[0] # e.g. [&#39;Measured1&#39;, &#39;Measured2&#39;]  </div>
<div>    csvOutFileName = easygui.filesavebox(title = &quot;Choose output file for averages&quot;, )  </div><div>    if csvOutFileName is not None:      </div><div>        print &quot;Saving using: &quot;+csvOutFileName  </div>
<div>        csvOut = file(csvOutFileName, &#39;rb&#39;)  </div><div>        writer = csv.writer(csvOut)  </div><div><b>        WRITER.WRITEROW(HEADINGS)</b></div><div>        for index in range(len(measured1)):  </div><div>
            writer.writerow([measured1[index], measured2[index]])  </div><div>        writer.writerow([averaged1, averaged2])  </div><div>    else:  </div><div>        print &quot;No filename for saving&quot;</div><div><br>
</div><div><br></div><div>so, my problem is I don&#39;t know why it keeps giving me this error. I&#39;ve checked on the internet, but I haven&#39;t found anything to help resolve this error.</div><div><br></div><div>I hope you can be of help.</div>
<div><br></div><div>PS: I&#39;ve added the complete code below for reference.</div><div><br></div><div>thanks </div></div><div><br></div><div><br></div><div><div>import csv  </div><div>import sys  </div><div>import easygui  </div>
<div>  </div><div>def getFileAndPath():  </div><div>    &quot;Get fully-qualified path to the csv file&quot;  </div><div>    # TODO argInitialFile = &#39;*.csv&#39;  </div><div>    fileAndPath = easygui.fileopenbox(title=&quot;Select .CSV file&quot;)  </div>
<div>    print &quot;Using:&quot;,fileAndPath   </div><div>    return fileAndPath  </div><div>  </div><div>def getLinesInCSVFile(fileAndPath):  </div><div>    &quot;read lines in CSV file, return a list of these lines&quot;  </div>
<div>    linesInCSV = []  </div><div>    reader = csv.reader(open(fileAndPath, &quot;rb&quot;))  </div><div>    for row in reader:  </div><div>        linesInCSV.append(row)  </div><div>    return linesInCSV  </div><div>  </div>
<div>def justNumbers(listOfStrings):  </div><div>    &quot;True if the list contains just numbers represented as strings&quot;  </div><div>    # e.g. [&#39;22.4&#39;, &#39;23.9&#39;]  </div><div>    isJustNumbers = True  </div>
<div>    for item in listOfStrings:          </div><div>        try:  </div><div>            nbr = float(item)              </div><div>        except ValueError:  </div><div>            isJustNumbers = False  </div><div>    return isJustNumbers  </div>
<div>  </div><div>def getNumbers(listOfStrings):  </div><div>    &quot;Convert a list of strings-of-numbers to a list of numbers, e.g. [&#39;22.4&#39;, &#39;23.9&#39;] -&gt; [22.4, 23.9]&quot;  </div><div>    numbers = []  </div>
<div>    for item in listOfStrings:  </div><div>        nbr = float(item)              </div><div>        numbers.append(nbr)  </div><div>    return numbers  </div><div>  </div><div>def average(values):  </div><div>    &quot;&quot;&quot;Computes the arithmetic mean of a list of numbers&quot;&quot;&quot;  </div>
<div>    return sum(values, 0.0) / len(values)  </div><div>  </div><div>if __name__ == &quot;__main__&quot;:  </div><div>    # get the file-name  </div><div>    #fileAndPath = getFileAndPath()  </div><div>    # NOTE quick hack to make our test/development process quicker  </div>
<div>    fileAndPath = &quot;c:\\testing\\measured2.csv&quot;</div><div>          </div><div>    # read the CSV file  </div><div>    linesInCSV = getLinesInCSVFile(fileAndPath)  </div><div>          </div><div>    measured1 = []  </div>
<div>    measured2 = []  </div><div>    for n in range(1,4):  </div><div>        line = linesInCSV[n]  </div><div>        isJustNumbers = justNumbers(line)  </div><div>        if not isJustNumbers:  </div><div>            print &quot;ERROR!  Expected a line of numbers, instead we got:&quot;,line</div>
<div>            sys.exit()   </div><div>        # we only get here if justNumbers reports that we only have numbers  </div><div>        # so we can extract the list of floating-point numbers  </div><div>        numbers = getNumbers(line)  </div>
<div>        measured1.append(numbers[0])  </div><div>        measured2.append(numbers[1])  </div><div>    averaged1 = average(measured1)  </div><div>    averaged2 = average(measured2)  </div><div>      </div><div>    # Show values of Measured1 in a choicebox  </div>
<div>    # We don&#39;t care about the choices, this is just for output  </div><div>    #easygui.choicebox(message = &quot;Sorted values in Measured1&quot;, title = &quot;Measured1&quot;, choices = measured1)  </div><div>
  </div><div>    headings = linesInCSV[0] # e.g. [&#39;Measured1&#39;, &#39;Measured2&#39;]  </div><div>    csvOutFileName = easygui.filesavebox(title = &quot;Choose output file for averages&quot;, )  </div><div>    if csvOutFileName is not None:      </div>
<div>        print &quot;Saving using: &quot;+csvOutFileName  </div><div>        csvOut = file(csvOutFileName, &#39;rb&#39;)  </div><div>        writer = csv.writer(csvOut)  </div><div>        writer.writerow(headings)  </div>
<div>        for index in range(len(measured1)):  </div><div>            writer.writerow([measured1[index], measured2[index]])  </div><div>        writer.writerow([averaged1, averaged2])  </div><div>    else:  </div><div>
        print &quot;No filename for saving&quot;</div></div></div>