In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Python how to read text files line by line", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python how to read text files line by line" bar!
There are several ways to read text files in Python.
In this article, I introduce the open () function, the read () method, the readline () method, the readlines () method, the close () method, and the with keyword.
What is the open () function in Python
If you want to read a text file in Python, you must first open it.
This is the basic syntax of the open () function of Python:
Open ("name of file you want opened", "optional mode") file name and correct path open ("demo.txt")
If the text file is in the same directory as your current file ("folder"), then you just need to reference the file name in the open () function.
The following is an example of two files in the same directory:
If your text file is in a different directory, you need to reference the correct pathname of the text file.
In this example, the random-text file is located in a different folder than main.py:
In order to access the file in main.py, you must include the folder name in the file name.
Open ("text-files/random-text.txt")
If you do not have the correct file path, you will receive the following error message:
Open ("random-text.txt")
It is important to keep track of your directory so that you can refer to the correct pathname.
Optional mode parameters in open ()
There are different modes when working with files. The default mode is read mode.
The letter r stands for read mode.
Open ("demo.txt", mode= "r")
You can also omit mode=, and just use "r".
Open ("demo.txt", "r")
There are other types of patterns, such as "w" for writing or "a" for appending. I won't go into the other patterns in detail, because we will only focus on reading files.
For a complete list of other modes, read the documentation.
Other parameters of the open () function in Python
The open () function can accept other optional parameters:
Buffering
Encoding
Errors
Newline
Closefd
Opener
Read the documentation for more parameters.
The readable () method in Python
If you want to check that the file is readable, you can use the readable () method. This returns either True or False.
This example will return True because we are in read mode:
File = open ("demo.txt") print (file.readable ())
If I change this example to "w" (write) mode, the readable () method returns False:
File = open ("demo.txt", "w") print (file.readable ())
The read () method in Python
The read () method reads all the contents of the file as a string. This is a good way to do this if there is not much content in the text file.
In this example, I use the read () method to print a list of names from the demo.txt file:
File = open ("demo.txt") print (file.read ())
This method can receive an optional parameter named size. Instead of reading the entire file, only part of it is read.
If we modify the previous example, we can print out only the first word by adding the number 4 as a parameter to read ().
File = open ("demo.txt") print (file.read (4))
If the size parameter is omitted, or if the number is negative, the entire file is read.
The close () method in Python
When you are finished reading the file, it is important to close it. If you forget to close the file, it may cause problems.
This is an example of how to close the demo.txt file:
File = open ("demo.txt") print (file.read ()) file.close ()
How to close a file in Python using the with keyword
One way to ensure that the file is closed is to use the with keyword. This is considered a good practice because the file closes automatically, and you don't have to close it manually.
Here is how to rewrite our example using the with keyword:
With open ("demo.txt") as file: the readline () method in print (file.read ()) Python
This method reads a line from the file and returns.
In this example, we have a text file that contains these two sentences:
This is the first lineThis is the second line
If we use the readline () method, it only prints the first sentence of the file.
With open ("demo.txt") as file: print (file.readline ())
This method also accepts optional size parameters. We can modify the example and add the number 7 to just read and print out the This is:
With open ("demo.txt") as file: print (file.readline (7))
The readlines () method in Python
This method reads and returns a list of all lines in the file.
In this example, we will use the readlines () method to print the grocery items as a list.
With open ("demo.txt") as file: print (file.readlines ())
How to use a for loop to read lines from a file in Python
An alternative to these different reading methods is to use a for loop.
In this example, we can print out all the items in the demo.txt file through a loop object.
With open ("demo.txt") as file: for item in file: print (item)
Summary
If you want to read a text file in Python, you must first open it.
Open ("name of file you want opened", "optional mode")
If the text file is in the same directory as your current file ("folder"), then you just need to reference the file name in the open () function.
If your text file is in a different directory, you need to reference the correct pathname of the text file.
The open () function accepts optional mode (mode) parameters. The default mode is read mode.
Open ("demo.txt", "r")
If you want to check that the file is readable, you can use the readable () method. This returns either True or False.
File.readable ()
The read () method reads all the contents of the file as a string.
File.read ()
When you are finished reading the file, it is important to close it. If you forget to close the file, it may cause problems.
File.close ()
One way to ensure that the file is closed is to use the with keyword.
With open ("demo.txt") as file: print (file.read ())
The readline () method reads a line from the file and returns.
File.readline ()
The readlines () method reads and returns a list of all the lines in the file.
File.readlines ()
An alternative to these different reading methods is to use a for loop.
With open ("demo.txt") as file: for item in file: print (item) Thank you for your reading, this is the content of "how to read the text file line by line by Python". After the study of this article, I believe you have a deeper understanding of how to read the text file line by line in Python. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.