2/25/22, 10:12 AM PY0101EN-4-1-ReadFile
Reading Files Python
Estimated time needed: 40 minutes
Objectives
After completing this lab you will be able to:
Read text files using Python libraries
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-4-1-ReadFile.ipynb?lti=true 1/9
2/25/22, 10:12 AM PY0101EN-4-1-ReadFile
Table of Contents
Download Data (https://download/?
utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm
SkillsNetwork-Channel-
SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01)
Reading Text Files (https://read/?
utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm
SkillsNetwork-Channel-
SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01)
A Better Way to Open a File (https://better/?
utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm
SkillsNetwork-Channel-
SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01)
Download Data
In [ ]:
import urllib.request
url = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkil
lsNetwork-PY0101EN-SkillsNetwork/labs/Module%204/data/example1.txt'
filename = 'Example1.txt'
urllib.request.urlretrieve(url, filename)
In [ ]:
# Download Example file
!wget -O /resources/data/Example1.txt https://cf-courses-data.s3.us.cloud-object-storage.a
ppdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%204/data/examp
le1.txt
Reading Text Files
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-4-1-ReadFile.ipynb?lti=true 2/9
2/25/22, 10:12 AM PY0101EN-4-1-ReadFile
One way to read or write a file in Python is to use the built-in open function. The open function provides a File
object that contains the methods and attributes you need in order to read, save, and manipulate the file. In this
notebook, we will only cover .txt files. The first parameter you need is the file path and the file name. An example
is shown as follow:
The mode argument is optional and the default value is r. In this notebook we only cover two modes:
**r**: Read mode for reading files
**w**: Write mode for writing files
For the next example, we will use the text file Example1.txt. The file is shown as follows:
We read the file:
In [ ]:
# Read the Example1.txt
example1 = "Example1.txt"
file1 = open(example1, "r")
We can view the attributes of the file.
The name of the file:
In [ ]:
# Print the path of file
file1.name
The mode the file object is in:
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-4-1-ReadFile.ipynb?lti=true 3/9
2/25/22, 10:12 AM PY0101EN-4-1-ReadFile
In [ ]:
# Print the mode of file, either 'r' or 'w'
file1.mode
We can read the file and assign it to a variable :
In [ ]:
# Read the file
FileContent = file1.read()
FileContent
The /n means that there is a new line.
We can print the file:
In [ ]:
# Print the file with '\n' as a new line
print(FileContent)
The file is of type string:
In [ ]:
# Type of file content
type(FileContent)
It is very important that the file is closed in the end. This frees up resources and ensures consistency across
different python versions.
In [ ]:
# Close file after finish
file1.close()
A Better Way to Open a File
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-4-1-ReadFile.ipynb?lti=true 4/9
2/25/22, 10:12 AM PY0101EN-4-1-ReadFile
Using the with statement is better practice, it automatically closes the file even if the code encounters an
exception. The code will run everything in the indent block then close the file object.
In [ ]:
# Open file using with
with open(example1, "r") as file1:
FileContent = file1.read()
print(FileContent)
The file object is closed, you can verify it by running the following cell:
In [ ]:
# Verify if the file is closed
file1.closed
We can see the info in the file:
In [ ]:
# See the content of file
print(FileContent)
The syntax is a little confusing as the file object is after the as statement. We also don’t explicitly close the file.
Therefore we summarize the steps in a figure:
We don’t have to read the entire file, for example, we can read the first 4 characters by entering three as a
parameter to the method .read():
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-4-1-ReadFile.ipynb?lti=true 5/9
2/25/22, 10:12 AM PY0101EN-4-1-ReadFile
In [ ]:
# Read first four characters
with open(example1, "r") as file1:
print(file1.read(4))
Once the method .read(4) is called the first 4 characters are called. If we call the method again, the next 4
characters are called. The output for the following cell will demonstrate the process for different inputs to the
method read() :
In [ ]:
# Read certain amount of characters
with open(example1, "r") as file1:
print(file1.read(4))
print(file1.read(4))
print(file1.read(7))
print(file1.read(15))
The process is illustrated in the below figure, and each color represents the part of the file read after the method
read() is called:
Here is an example using the same file, but instead we read 16, 5, and then 9 characters at a time:
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-4-1-ReadFile.ipynb?lti=true 6/9
2/25/22, 10:12 AM PY0101EN-4-1-ReadFile
In [ ]:
# Read certain amount of characters
with open(example1, "r") as file1:
print(file1.read(16))
print(file1.read(5))
print(file1.read(9))
We can also read one line of the file at a time using the method readline() :
In [ ]:
# Read one line
with open(example1, "r") as file1:
print("first line: " + file1.readline())
We can also pass an argument to readline() to specify the number of charecters we want to read.
However, unlike read() , readline() can only read one line at most.
In [ ]:
with open(example1, "r") as file1:
print(file1.readline(20)) # does not read past the end of line
print(file1.read(20)) # Returns the next 20 chars
We can use a loop to iterate through each line:
In [ ]:
# Iterate through the lines
with open(example1,"r") as file1:
i = 0;
for line in file1:
print("Iteration", str(i), ": ", line)
i = i + 1
We can use the method readlines() to save the text file to a list:
In [ ]:
# Read all lines and save as a list
with open(example1, "r") as file1:
FileasList = file1.readlines()
Each element of the list corresponds to a line of text:
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-4-1-ReadFile.ipynb?lti=true 7/9
2/25/22, 10:12 AM PY0101EN-4-1-ReadFile
In [ ]:
# Print the first line
FileasList[0]
Print the second line
FileasList[1]
In [ ]:
# Print the third line
FileasList[2]
The last exercise!
Congratulations, you have completed your first lesson and hands-on lab in Python.
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-4-1-ReadFile.ipynb?lti=true 8/9
2/25/22, 10:12 AM PY0101EN-4-1-ReadFile
Author
Joseph Santarcangelo (https://www.linkedin.com/in/joseph-s-50398b136/?
utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA
SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-
2021-01-01)
Other contributors
Mavis Zhou (https://www.linkedin.com/in/jiahui-mavis-zhou-a4537814a?
utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA
SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-
2021-01-01)
Change Log
Date (YYYY-MM-DD) Version Changed By Change Description
2022-01-10 2.1 Malika Removed the readme for GitShare
2020-09-30 1.3 Malika Deleted exericse "Weather Data"
2020-09-30 1.2 Malika Singla Weather Data dataset link added
2020-09-30 1.1 Arjun Swani Added exericse "Weather Data"
2020-09-30 1.0 Arjun Swani Added blurbs about closing files and read() vs readline()
2020-08-26 0.2 Lavanya Moved lab to course repo in GitLab
© IBM Corporation 2020. All rights reserved.
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-4-1-ReadFile.ipynb?lti=true 9/9