Python – Reading text files

In this post we will creating a Python script that will read from a text file, to do this we will be using a with statement which will run through and print the lines of text within a file. The with statement is especially useful as it will automatically close the file once done.

See the snippet of code below.

with open("textFile.txt", encoding='utf-8') as file:
    my_data = file.read()
    print(my_data)

Leave a Reply