Python Forum
search and store a value from linux command output in to variable python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
search and store a value from linux command output in to variable python
#1
Hi I am a python bigginer, I have a scenario where I need to search and select a particular value from linux command "df -h".

dumpe2fs 1.42.13.x5 (23-Mar-2017)
Inode count: 786432000

In this above output I need to store the inode count number in to a variable to use it for further calculation.
Can anyone explain how to use python regular expressions to do the above task?
Reply
#2
Get the output using subprocess.

import subprocess

output = subprocess.run(['df', '-h'], stdout=subprocess.PIPE).stdout
output = output.decode('utf-8')

for line in output.split('\n'):
    if line.startswith('Inode count:'):
        inode_cnt = int(line.split()[1])
Something like that.
Btw, I don't get the number of inodes with df -h.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 633 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,034 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  store all variable values into list and insert to sql_summary table mg24 3 1,097 Sep-28-2022, 09:13 AM
Last Post: Larz60+
  How to combine two output in one variable? ilknurg 2 1,144 Aug-01-2022, 09:31 AM
Last Post: Gribouillis
  How to write a part of powershell command as a variable? ilknurg 2 1,084 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  Os command output in variable shows wrong value paulo79 2 1,467 Apr-09-2022, 03:48 PM
Last Post: ndc85430
  How to use a variable in linux command in python code? ilknurg 2 1,547 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  Search text in PDF and output its page number. atomxkai 21 8,679 Jan-21-2022, 06:20 AM
Last Post: snippsat
  Store variable data and display sum after 60 seconds the_dude 11 3,367 Dec-16-2021, 07:07 PM
Last Post: deanhystad
  use subprocess on linux\pi wwith a "grep " command korenron 2 7,906 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020