Python Tutorials

Overview

In Python, the open() function will open the file and the read() method is used to read the content of the file.

file = open("myfile.txt", "r")
print(file.read())

Note

In this example myfile.txt is located in the same directory as our Python file. If it is in a different directory, the full path to the directory must be specified.

Output:

Hello World! #the full contents of myfile.txt will appear here

Using the with Statement

The with statement can also be used to open and read a file.

with open("myfile.txt", "r") as file:
    print(file.read())

Output:

Hello World! #the full contents of myfile.txt will appear here

Closing the File

It is a good practice to close the file when you are done with it.

file = open("myfile.txt", "r")
print(file.readline())

file.close()

Note

When using the with statement, there is no need to close your file. It is self closing.

Note

You should always close your files because changes made to a file may not show until the file is closed.

Output:

Hello World!

Reading Only Parts of the File

By default the read() method returns the contents of the entire file. But it can also be specified how many characters should be returned.

with open("myfile.txt", "r") as file:
    print(filer.read(5))

Output:

Hello

Reading a File Line by Line

The readline() method can be used to read a file one line at a time when necessary

with open("myfile.txt") as file:
    print(file.readline())

Output:

Hello World!

To read multiple lines, use the readline() method multiple times.

with open("myfile.txt") as file:
    print(file.readline())
    print(file.readline())

Output:

Hello World!
Have a wonderful day.

Looping Through the Lines of a File

By looping through the lines of the file, the entire file can be read line by line.

with open("myfile.txt") as file:
    for x in file:
        print(x)

Output:

Hello World!
Have a wonderful day.
And thanks for checking out "Python - Reading Files".

Python Notes:

  • The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
  • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
  • Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
  • Python string methods return new values, and DO NOT change the original string
  • Python tuples are unchangeable after created (their items CANNOT be changed or re-ordered at a later point)
  • Python sets are unordered (may appear in random orders when called), unchangeable (the value of individual items cannot be changed after creation), unindexed (items cannot be referred to by index or key), and duplicates are NOT ALLOWED
  • As of v3.7, Python dictionaries are ordered and duplicates ARE ALLOWED; in v3.6 and earlier, dictionaries were unordered (did not have a defined order and could not be referred to using an index)
  • Python does not have built-in support for arrays, but Python lists can be used as pseudo “arrays”; therefore, all Python list methods will work with these pseudo “arrays”

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.