with open('file.txt', 'r') as file: text = file.read() print(text)
with open('file.txt', 'w') as file: file.write('Hello, world!\n') file.write('This is a new line.')In this example, `file.txt` is opened in write mode and the `write()` method is used to write two lines of text to the file. The `with` statement ensures that the file is closed after the operation is completed. Package library: Python standard library.