How To Get Data Of File Python
Summary: in this tutorial, you learn various ways to read text files in Python.
TL;DR
The following shows how to read all texts from the readme.txt
file into a cord:
with open up('readme.txt') every bit f: lines = f.readlines()
Code linguistic communication: JavaScript ( javascript )
Steps for reading a text file in Python
To read a text file in Python, you follow these steps:
- First, open a text file for reading by using the
open up()
part. - Second, read text from the text file using the file
read()
,readline()
, orreadlines()
method of the file object. - Third, close the file using the file
close()
method.
1) open up() part
The open up()
function has many parameters only you'll be focusing on the commencement 2.
open(path_to_file, mode)
The path_to_file
parameter specifies the path to the text file.
If the file is in the aforementioned folder as the program, you merely demand to specify the proper name of the file. Otherwise, y'all need to specify the path to the file.
To specify the path to the file, yous utilise the forward-slash ('/'
) even if you lot're working in Windows.
For instance, if the file is readme.txt
stored in the sample binder every bit the programme, you need to specify the path to the file every bit c:/sample/readme.txt
The way
is an optional parameter. Information technology's a string that specifies the mode in which you want to open the file.
The following table shows available modes for opening a text file:
Mode | Clarification |
---|---|
'r' | Open for text file for reading text |
'due west' | Open a text file for writing text |
'a' | Open a text file for appending text |
For example, to open a file whose proper name is the-zen-of-python.txt
stored in the same folder as the program, you apply the post-obit lawmaking:
f = open('the-zen-of-python.txt','r')
Code language: JavaScript ( javascript )
The open()
role returns a file object which you will utilize to read text from a text file.
two) Reading text methods
The file object provides you 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 you lot have a small file and y'all want to manipulate the whole text of that file. -
readline()
– read the text file line past line and render all the lines as strings. -
readlines()
– read all the lines of the text file and return them as a list of strings.
three) close() method
The file that you open will remain open up until you close it using the close() method.
It'southward important to shut the file that is no longer in employ. If y'all don't shut the file, the program may crash or the file would exist corrupted.
The post-obit shows how to call the close()
method to close the file:
f .shut()
Code language: CSS ( css )
To close the file automatically without calling the shut()
method, y'all use the with
argument like this:
with open(path_to_file) equally f: contents = f.readlines()
Code language: JavaScript ( javascript )
In do, you'll use the with
statement to close the file automatically.
Reading a text file examples
Nosotros'll use the-zen-of-python.txt file for the sit-in.
The post-obit example illustrates how to use the read()
method to read all the contents of the the-zen-of-python.txt
file into a string:
with open up('the-zen-of-python.txt') every bit f: contents = f.read() impress(contents)
Lawmaking language: JavaScript ( javascript )
Output:
Beautiful is better than ugly. Explicit is improve than implicit. Simple is better than complex. ...
The following example uses the readlines()
method to read the text file and returns the file contents as a list of strings:
lines = [] with open('the-zen-of-python.txt') as f: lines = f.readlines() count = 0 for line in lines: count += i print(f'line {count}: {line}')
Lawmaking linguistic communication: JavaScript ( javascript )
Output:
line 1: Beautiful is improve than ugly. line 2: Explicit is better than implicit. line three: Simple is better than complex. ...
The following example shows how to employ the readline()
to read the text file line by line:
with open('the-zen-of-python.txt') equally f: line = f.readline() while line: line = f.readline() impress(line)
Code language: JavaScript ( javascript )
Output:
Explicit is better than implicit. Simple is meliorate than circuitous. Circuitous is meliorate than complicated. ...
A more concise way to read a text file line past line
The open()
function returns a file object which is an iterable object. Therefore, you can use a for
loop to iterate over the lines of a text file as follows:
with open('the-zen-of-python.txt') as f: for line in f: print(line)
Code language: JavaScript ( javascript )
This is more curtailed fashion to read a text file line by line.
Read UTF-8 text files
The code in the previous examples works fine with ASCII text files. However, if you lot're dealing with other languages such as Japanese, Chinese, and Korean, the text file is not a simple ASCII text file. And information technology'southward likely a UTF-eight file that uses more than than but the standard ASCII text characters.
To open a UTF-8 text file, you need to laissez passer the encoding='utf-8'
to the open()
function to instruct it to expect UTF-8 characters from the file.
For the demonstration, yous'll use the post-obit quotes.txt
file that contains some quotes in Japanese.
The post-obit shows how to loop through the quotes.txt
file:
with open up('quotes.txt', encoding='utf8') as f: for line in f: print(line.strip())
Code language: JavaScript ( javascript )
Output:
Summary
- Use the
open up()
function with the'r'
mode to open up a text file for reading. - Use the
read()
,readline()
, orreadlines()
method to read a text file. - Always close a file after completing reading information technology using the
close()
method or thewith
argument. - Employ the
encoding='utf-8'
to read the UTF-8 text file.
Did you detect this tutorial helpful ?
How To Get Data Of File Python,
Source: https://www.pythontutorial.net/python-basics/python-read-text-file/
Posted by: fairleyhusith.blogspot.com
0 Response to "How To Get Data Of File Python"
Post a Comment