With open in Python

We use the open() method to create a file object that allows us to read, write and append to files.

The general syntax is as follows:

file_object = open(<file_name>, <access_mode>)

<file_name> is a path to the file you want to read, create or modify. The <access_mode> denotes the mode in which to open the file. The most frequently used are:

When we have finished with an I/O operation, such as reading from or writing to a file, we must call file.close() to terminate the process. This removes the reference to the file from memory.

A more pythonic and concise way of reading files and closing them is to use with...as syntax. When this phrasing is used, a self-contained context is created for the I/O operation that closes the file automatically.

with open('filename.txt', 'r') as file:
    contents = file.read()
    print(contents)