Python read last lines in file

Read File in Python

In this article, we’ll learn how to read files in Python.

In Python, temporary data that is locally used in a module will be stored in a variable. In large volumes of data, a file is used such as text and CSV files and there are methods in Python to read or write data in those files.

After reading this tutorial, you’ll learn: –

  • Reading both text and binary files
  • The different modes for reading the file
  • All methods for reading a text file such as read() , readline() , and readlines()
  • Read text file line by line
  • Read and write files at the same time.

Table of contents

Access Modes for Reading a file

To read the contents of a file, we have to open a file in reading mode. Open a file using the built-in function called open() . In addition to the file name, we need to pass the file mode specifying the purpose of opening the file.

Читайте также:  Java private constructor final

The following are the different modes for reading the file. We will see each one by one.

Read text file

# read file with absolute path try: fp = open(r"E:\demos\files\read_demo.txt", "r") print(fp.read()) fp.close() except FileNotFoundError: print("Please check the path")
First line Second line Third line Fourth line Fifth line

An absolute path contains the entire path to the file or directory that we need to access. It includes the complete directory list required to locate the file.

For example, E:\PYnative\files_demos\read_demo.txt is an absolute path to discover the read_demo.txt. All of the information needed to find the file is contained in the path string.

While opening a file for reading its contents we have always ensured that we are providing the correct path. In case the file not present in the provided path we will get FileNotFoundError .

We can avoid this by wrapping the file opening code in the try-except-finally block.

Reading a File Using the with Statement

We can open a file using the with statement along with the open function. The general syntax is as follows.

with open(__file__, accessmode) as f:

The following are the main advantages of opening a file using ‘with’ statement

  • The with statement simplifies exception handling by encapsulating common preparation and cleanup tasks.
  • This also ensures that a file is automatically closed after leaving the block.
  • As the file is closed automatically it ensures that all the resources that are tied up with the file are released.

Let us see how we can the with statement to read a file.

# Reading files using 'with' with open('read_demo.txt', 'r') as file: print(file.read())
First line Second line Third line Fourth line Fifth line

File Read Methods

Python provides three different methods to read the file. We don’t have to import any module for that.. Below are the three methods

Text file after read and write operation

Reading File in Reverse Order

We can read the contents of the file in reverse order by using the readlines() method and then calling the reversed () method on the list to get the contents of the list in reverse order. We can then iterate over the contents of the list and print the values.

with open('readdemo.txt', 'r') as f: lines = f.readlines() for line in reversed(lines): print(line)
Fifth Line Fourth Line Third Line Second Line First Line

Reading a Binary file

Binary files are basically the ones with data in the Byte format (0’s and 1’s). This generally doesn’t contain the EOL(End of Line) so it is important to check that condition before reading the contents of the file.

We can open and read the contents of the binary file using the ‘with’ statement as below.

with open("Timezones.jpg", "rb") as f: byte_content = f.read(1) while byte_content: #Printing the contents of the file print(byte_content)

We have seen in this post how the file contents could be read using the different read methods available in Python. We also saw few simple examples to read the contents partially like first few lines or last few lines based on our requirement.

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

About Vishal

I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ

Источник

How to Read the Last Line of a File in Python

To read the last line of a file in Python without storing the entire file in memory:

  1. Loop through each line in the file.
  2. Skip each line until you reach the last line.
  3. Save the last line in memory.
with open("filename.txt") as file: for line in file: pass last_line = line

The Naive Approach

The most naive approach to read the last line of a file in Python is:

  1. Read the entire file line by line into a list.
  2. Access the last element of that list.
with open("example.txt", "r") as file: last_line = file.readlines()[-1]

Find the Last Line of a Large File in Python

If you have a big file you don’t want to store in memory or loop through the way you saw previously, you can rely on seeking the file.

To find the last line of a large file in Python by seeking:

  1. Open the file in binary mode.
  2. Seek to the end of the file.
  3. Jump back the characters to find the beginning of the last line.

Here is how it looks in code:

import os with open("example.txt", "rb") as file: try: file.seek(-2, os.SEEK_END) while file.read(1) != b'\n': file.seek(-2, os.SEEK_CUR) except OSError: file.seek(0) last_line = file.readline().decode()

However, by looking at the code, it is not trivial to understand how it works.

How Does File Seeking Work in Python

To read the last line of a file by seeking in Python, the file reader is moved at the very last character of the file. Then the reader jumps backward character by character until it reaches the beginning of the last line. Then it reads the last line into memory.

Here is an illustration of how the code finds the last line in a file with three lines of text:

Let’s take a look at the code in a bit more detail.

First of all, seeking means setting the file reader’s position in the file.

In Python, you can only seek binary files, so you need to read the file as a binary file.

The seek() method follows this syntax:

The offset specifies how many characters to move. Use a negative value to move backward.

The whence argument is an optional argument that can be set

  • os.SEEK_SET or 0—seek relative to the begining of the file.
  • os.SEEK_CUR or 1—seek relative to the current position in the file.
  • os.SEEK_END or 2—seek relative to the end of the file.

First of all, let’s simplify the above code a bit by removing the error handling, and the os module constants:

with open("example.txt", "rb") as file: file.seek(-2, 2) while file.read(1) != b'\n': file.seek(-2, 1) last_line = file.readline().decode()

Now that you understand how the seek() method works, you can understand the code:

  • file.seek(-2, 2) means “Jump to the 2nd character before the end of the file”.
  • file.read(1) means “Read the character from the right”.
  • file.seek(-2, 1) means “Jump 2 characters left from the current character”.
  • b’\n’ is the line break character in binary mode.
  • file.readline().decode() reads the line and converts it from binary to string.

So the process is as follows:

  • The code first jumps to the very last character of the file.
  • It then checks if the character is a line break character.
  • If it is not, then it jumps back one character and checks again.
  • It repeats this until the line break is encountered, which suggests that the beginning of the last line is reached.
  • Then it stores the last line in memory.

Notice that seeking a file only works with binary files. This is why the file is read in binary mode. Also, notice that as you are dealing with a binary file, you need to jump two characters left to read one character to the right.

Now, let’s bring back the try-catch error handling to make sure the file is not exactly one line long. Because if it is, the seek() will never encounter a line break character as there are none. Let’s also use the os module’s constants SEEK_END and SEEK_CUR instead of plain 1 or 2:

import os with open("example.txt", "rb") as file: try: file.seek(-2, os.SEEK_END) while file.read(1) != b'\n': file.seek(-2, os.SEEK_CUR) except OSError: file.seek(0) last_line = file.readline().decode()

Now you learned how the seeking approach works when reading the last line of a file in Python.

Conclusion

Today you learned 3 ways to read the last line of a file in Python:

  1. Skip through the file all the way to the last line.
  2. Read the entire file in memory, and access the last line.
  3. Seek to the end of the file and read the last line.

Pick one that best fits your needs.

I hope you find it useful. Thanks for reading!

Источник

Read Last Line of File Using Python

Read Last Line of File Using Python

  1. Read Last Line of File With the for Loop in Python
  2. Read Last Line of File With the readlines() Function in Python

This tutorial will discuss the methods to read the last line from a file in Python.

Read Last Line of File With the for Loop in Python

The for loop is used to iterate through each element of an iterable in Python. We can use the for loop to iterate through each line inside a file sequentially and then read the last line of the file. The following code snippet shows us how to read the last line of a file with the for loop.

with open('file.txt', 'r') as f:  for line in f:  pass  last_line = line print(last_line) 

We opened the file.txt file in the read mode and used the for loop to iterate through each line in the file. We used the pass keyword to keep the loop empty. This pass keyword acts as a blank line in Python and is used when we don’t want to write any code inside a loop or a conditional statement. We store the last line inside the last_line variable when the loop ends and print its value.

Read Last Line of File With the readlines() Function in Python

The file.readlines() function reads all the lines of a file and returns them in the form of a list. We can then get the last line of the file by referencing the last index of the list using -1 as an index. The following code example shows us how to read the last line of a file with Python’s file.readlines() function.

with open('file.txt', 'r') as f:  last_line = f.readlines()[-1] print(last_line) 

We opened the file.txt file in the read mode and used the f.readlines()[-1] to read the last line of the file. We used [-1] because the readlines() function returns all the lines in the form of a list, and this [-1] index gives us the last element of that list.

In Python, no method can directly read the last line of a file. So, we have to read the whole file sequentially until we reach the last line. The first method reads the file line by line, while the second method reads all the lines simultaneously.

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

Related Article — Python File

Источник

Оцените статью