---
title: "How to Write Logic in Python Equals to Linux [Tail -1]"
description: "You can write a program in python equal to Linux's tail command. Here are the steps explained for your quick reading."
url: https://srinimf.com/2022/11/07/python-program-equal-to-linux-tail-1-command/
date: 2022-11-07
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2022/11/pexels-photo-1708912.jpeg?fit=1880%2C1253&ssl=1
categories: ["Python"]
tags: ["Program equal to tail command"]
type: post
lang: en
---

# How to Write Logic in Python Equals to Linux [Tail -1]

Here’s a sample python program, which works as the Linux tail -1 command. Python’s text file data you can read and update using [file commands](https://srinimf.com/2017/02/25/top-13-proven-unix-commands-absolutely-helpful-for-files/). Here’s a magical python program for your reference.

**Table of contents**

1. [How to read a file in python](https://srinimf.com/2022/11/07/python-program-equal-to-linux-tail-1-command/#how-to-read-a-file-in-python)
2. [Read file records and store in an object](https://srinimf.com/2022/11/07/python-program-equal-to-linux-tail-1-command/#read-file-records-and-store-in-an-object)
3. [Python logic equals to Linux tail -1](https://srinimf.com/2022/11/07/python-program-equal-to-linux-tail-1-command/#python-logic-equals-to-linux-tail-1)

## How to read a file in python

With the readline() or readlines() method, you can read file contents in python. After you set read mode, you need the for loop to read records. Begin and end of the positions, if you specify, you can read. It is not surprising.

The surprising point is, if you decide to read only the last record, how to get it is a surprise for you.

## Read file records and store in an object

The Below code sets the python file to read mode. And it reads all the rows.

```
# Open the file and read in all of the lines

with open('file1.txt', 'r') as f:
    lines = f.readlines()
print(lines)
```

The output displays all three records. This’s just normal.

```
['1111111111111\n', '222222222222\n', '3333333333']
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

Related[The True Difference Between Discrete and Continuous data](https://srinimf.com/2022/10/24/the-exclusive-differences-between-discrete-and-continuous-data/)

## Python logic equals to Linux tail -1

When you give the** tail -1** [[linuxfoundation.org](https://www.linuxfoundation.org/blog/blog/classic-sysadmin-14-tail-and-head-commands-in-linux-unix)] command, it shows the last record. Similarly, the python program works. We will see how.

The input file **file1.txt** has three records. The data of the input file is as below.

```
#file1.txt
1111111111111
222222222222
3333333333
```

So, when you use the below logic, you will get the last record. That is 3333333333.

```
# Open the file and read in all of the lines

with open('file1.txt', 'r') as f:
    lines = f.readlines()

 # Now, get the ones they want to see
    for i in lines[len(lines)-1:]:
 # Strip off trailing newline
        print(i[0:len(i)-1])
```

The output shows the last record.

```
333333333

** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

Read

- [Python Indentation Rules Handy Pdf](https://srinimf.com/2019/10/13/python-code-indentation-rules-handy-guide/)DateOctober 13, 2019
- [3 Top Python Commands to Clear Screen](https://srinimf.com/2018/01/19/three-really-working-commands-to-clear-python-console/)DateJanuary 19, 2018
- [Pycache Folder in Python: How to Access its Location](https://srinimf.com/2024/02/23/pycache-folder-in-python-how-to-access-its-location/)DateFebruary 23, 2024

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2022%2F11%2F07%2Fpython-program-equal-to-linux-tail-1-command%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2022%2F11%2Fpexels-photo-1708912.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=How%20to%20Write%20Logic%20in%20Python%20Equals%20to%20Linux%20%5BTail%20-1%5D)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2022/11/07/python-program-equal-to-linux-tail-1-command/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2022/11/07/python-program-equal-to-linux-tail-1-command/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2022/11/07/python-program-equal-to-linux-tail-1-command/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2022/11/07/python-program-equal-to-linux-tail-1-command/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Write%20Logic%20in%20Python%20Equals%20to%20Linux%20%5BTail%20-1%5D&body=https%3A%2F%2Fsrinimf.com%2F2022%2F11%2F07%2Fpython-program-equal-to-linux-tail-1-command%2F&share=email)
-

### Like this:

Like Loading…
