Python File I/O: Read last n lines of a file
4. Read Last N Lines
Write a Python program to read last n lines of a file.
Sample Solution:-
Python Code:
Sample Output:
Append this text. Append this text.
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Python program to read the last n lines of a file using file seek operations.
- Write a Python program to read the last n lines of a large file without loading the entire file into memory.
- Write a Python program to read the last n non-empty lines of a file and print them in reverse order.
- Write a Python program to read the last n lines of a file and calculate the total number of characters in those lines.
Go to:
Previous: Write a Python program to append text to a file and display the text.
Next: Write a Python program to read a file line by line and store it into a list.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Based on 136 votes, average difficulty level of this exercise is Hard
.
Test your Programming skills with w3resource's quiz.