how to print to a file in python 1

how to print to a file in python

print("Hello stackoverflow!", file=open("output.txt", "a"))
print("I have a question.", file=open("output.txt", "a"))

Here is what the above code is Doing:
1. It opens the file in append mode.
2. It prints the string to the file.
3. It closes the file.

The file is opened in append mode, so the file is not truncated before writing.

Similar Posts