Bạn có thể chỉ cần ghi dòng vào tệp bằng chức năng ghi.
Ví dụ
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 Ngoài ra, bạn có thể sử dụng hàm print () có sẵn kể từ Python 2.6+
from __future__ import print_function # Only needed for Python 2
print("hi there", file=f) Đối với Python 3, bạn không cần nhập, vì hàm print () là mặc định.