Read a File in Python

In this tutorial, we will introduce the way to read a file using python.

In order to read a file in python, we can use python with statement.

1. Prepare a file to read

#!/usr/bin/env python

# Define a filename.
filename = "test.py"

2. Use python with statement to read a file

with open(filename, 'r') as f:
    content = f.read()
print(content)