Python: Write to File

By Xah Lee. Date: . Last updated: .

To open a file and write to it, do:

import sys

# 'w' for write (overwrite exsiting file), 'a' for append
xfile = open("c:/Users/xah/x126238.html", "w", encoding="utf-8")

xfile.write("hello\n")

xfile.close()

print("done")

Python Text Processing