To write a line to a file you should use the print() function which is available since the release of Python 2.6+:-
from __future__ import print_function # Only needed for Python 2
print("hi there", file=f)
In Python 3 we have print() function as default so we don't need the import it:-
f = open('myfile', 'w')
f.write('hi there\n') # python will convert \n to os.linesep f.close() # you can omit in most cases as the destructor will call it
To know more about this you can have a look at the following video tutorial:-