The with construct makes sure the file is closed afterwards.
with open( fileName, "a") as f: for line in lines: f.write( line)