I prefer this solution using the csv module from the standard library and the with statement to keep the file open.
The key point is to use 'a' to add when opening a file.
import csv fields=['first','second','third'] with open(r'name', 'a') as f: writer = csv.writer(f) writer.writerow(fields)
If you are using Python 2.7, you might get extra new lines on Windows. You can try to avoid them by using 'ab' instead of 'a' , however this will raise a TypeError: a byte-like object is required, not 'str' in python and CSV in Python 3.6 when adding newline='' as Natasha said in Python 3.6, You will encounter problems of backward compatibility of the text binary mode of the Python 2 and 3 csv module .
GM Jun 06 '16 at 9:46 on 2016-06-06 09:46
source share