Add file name as last column of CSV file
Question: I have a Python script which modifies a CSV file to add the filename as the
last column:

    
    
    import sys
    import glob
    
    for filename in glob.glob(sys.argv[1]):
        file = open(filename)
        data = [line.rstrip() + "," + filename for line in file]
        file.close()
    
        file = open(filename, "w")
        file.write("\n".join(data))
        file.close()
    

Unfortunately, it also adds the filename to the header (first) row of the
file. I would like the string "ID" added to the header instead. Can anybody
suggest how I could do this?


Answer: Have a look at the official [csv
module](http://docs.python.org/library/csv.html#examples).

