Read Lines of Text File Into List Python
Summary: in this tutorial, you learn various ways to read text files in Python.
TL;DR
The post-obit shows how to read all texts from the readme.txt
file into a cord:
with open('readme.txt') as f: lines = f.readlines()
Code language: JavaScript ( javascript )
Steps for reading a text file in Python
To read a text file in Python, you follow these steps:
- Commencement, open a text file for reading past using the
open()
role. - Second, read text from the text file using the file
read()
,readline()
, orreadlines()
method of the file object. - Tertiary, shut the file using the file
close()
method.
1) open() function
The open up()
function has many parameters but yous'll be focusing on the first two.
open(path_to_file, fashion)
The path_to_file
parameter specifies the path to the text file.
If the file is in the same folder every bit the program, you only demand to specify the name of the file. Otherwise, you need to specify the path to the file.
To specify the path to the file, y'all use the forrard-slash ('/'
) even if you're working in Windows.
For example, if the file is readme.txt
stored in the sample binder as the program, you need to specify the path to the file equally c:/sample/readme.txt
The way
is an optional parameter. It'due south a cord that specifies the mode in which you lot want to open the file.
The following table shows available modes for opening a text file:
Mode | Description |
---|---|
'r' | Open for text file for reading text |
'west' | Open a text file for writing text |
'a' | Open up a text file for appending text |
For instance, to open a file whose name is the-zen-of-python.txt
stored in the same folder as the plan, you use the following code:
f = open('the-zen-of-python.txt','r')
Code linguistic communication: JavaScript ( javascript )
The open()
role returns a file object which you will employ to read text from a text file.
two) Reading text methods
The file object provides y'all with three methods for reading text from a text file:
-
read()
– read all text from a file into a string. This method is useful if y'all have a pocket-sized file and you want to dispense the whole text of that file. -
readline()
– read the text file line by line and return all the lines as strings. -
readlines()
– read all the lines of the text file and return them as a list of strings.
3) close() method
The file that you open will remain open up until you lot close it using the close() method.
It'due south of import to close the file that is no longer in use. If you lot don't shut the file, the program may crash or the file would exist corrupted.
The following shows how to call the close()
method to close the file:
f .close()
Code linguistic communication: CSS ( css )
To close the file automatically without calling the close()
method, you lot use the with
argument similar this:
with open(path_to_file) as f: contents = f.readlines()
Code language: JavaScript ( javascript )
In practice, you'll use the with
statement to shut the file automatically.
Reading a text file examples
We'll use the-zen-of-python.txt file for the demonstration.
The following case illustrates how to utilise the read()
method to read all the contents of the the-zen-of-python.txt
file into a string:
with open('the-zen-of-python.txt') every bit f: contents = f.read() print(contents)
Code language: JavaScript ( javascript )
Output:
Beautiful is ameliorate than ugly. Explicit is better than implicit. Simple is better than complex. ...
The following instance uses the readlines()
method to read the text file and returns the file contents as a listing of strings:
lines = [] with open('the-zen-of-python.txt') equally f: lines = f.readlines() count = 0 for line in lines: count += 1 impress(f'line {count}: {line}')
Lawmaking language: JavaScript ( javascript )
Output:
line 1: Beautiful is ameliorate than ugly. line 2: Explicit is meliorate than implicit. line 3: Simple is better than complex. ...
The post-obit example shows how to use the readline()
to read the text file line by line:
with open('the-zen-of-python.txt') every bit f: line = f.readline() while line: line = f.readline() impress(line)
Code language: JavaScript ( javascript )
Output:
Explicit is improve than implicit. Simple is better than circuitous. Complex is better than complicated. ...
A more concise way to read a text file line by line
The open()
office returns a file object which is an iterable object. Therefore, you tin use a for
loop to iterate over the lines of a text file equally follows:
with open('the-zen-of-python.txt') every bit f: for line in f: print(line)
Code language: JavaScript ( javascript )
This is more concise mode to read a text file line past line.
Read UTF-eight text files
The code in the previous examples works fine with ASCII text files. However, if you're dealing with other languages such as Japanese, Chinese, and Korean, the text file is not a uncomplicated ASCII text file. And it's likely a UTF-8 file that uses more than but the standard ASCII text characters.
To open up a UTF-8 text file, you demand to pass the encoding='utf-8'
to the open()
function to instruct it to expect UTF-viii characters from the file.
For the demonstration, y'all'll use the following quotes.txt
file that contains some quotes in Japanese.
The following shows how to loop through the quotes.txt
file:
with open('quotes.txt', encoding='utf8') as f: for line in f: impress(line.strip())
Code language: JavaScript ( javascript )
Output:

Summary
- Use the
open()
function with the'r'
mode to open a text file for reading. - Use the
read()
,readline()
, orreadlines()
method to read a text file. - Ever close a file after completing reading it using the
close()
method or thewith
statement. - Use the
encoding='utf-8'
to read the UTF-8 text file.
Did you find this tutorial helpful ?
Source: https://www.pythontutorial.net/python-basics/python-read-text-file/
0 Response to "Read Lines of Text File Into List Python"
Post a Comment