with open('output.txt', 'w') as f: f.write('Hello, World!') f.flush()
import time with open('output.txt', 'a') as f: for i in range(10): f.write('Line {}\n'.format(i)) time.sleep(1) f.flush()In this example, we open a file named `output.txt` for appending and write 10 lines to it in a loop. We then call the `sleep()` function from the `time` module to pause the program for 1 second between each write. Finally, we call the `flush()` method after each write to ensure that the contents of the buffer are written to the file at regular intervals. The `flush()` method is part of the `TextIOWrapper` class, which is part of the `io` package in Python.