Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Command output to Variable
#1
Good afternoon everyone. I am new to python and I am trying to get my self up to speed as soon as possible. I thought it would be easy since ive already worked with VBS and Powershell but this seems like a whole new animal. so what I am trying to do is i want to run a command in shell then take the output of that command and load it onto a variable. I then want to take the output and do a for each loop so i can add an additional if statement to look for specific syntax and weed out what i want from what i dont. however each time i have tried it i cant find a way to read one line at a time it just gives me the entire output so i am not sure what im doing wrong. it is possible that the tool I am using to generate the output is at fault but i dont se how yet. any assistance you can provide would be greatly appreciated. below is one of the pieces of code ive used that partially succeeds. just fyi the msecli -L command is a Micron tool used to gather Nvme HDD status info. this is the data i am trying to collect.

import time
import sys
import subprocess
#subprocess.call('msecli -L', shell=true)
proc=subprocess.Popen('msecli -L', shell=True, stdout=subprocess.PIPE, )
output=proc.communicate()[0]
print(output)


driveoutputA = [ str(output, 'utf-8') ]

print(driveoutputA)


for val in str(driveoutputA, 'utf-8'): 

print(val, end=' ')

time.sleep(3) # Sleep for 3 seco
Yoriz write Aug-26-2021, 09:04 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
The output is just the STDOUT as a str. If you want to treat it as lines, just split on newlines. Also, it will be returned as bytes, so decoding it back to str is often appropriate.

I would probably use .run() instead, but that's minor. The code above would work just fine once the output is split.
import subprocess

cmd = "/bin/ls"
p = subprocess.run(cmd, capture_output=True)
lines = p.stdout.decode().splitlines()
print(f"{cmd} output contained {len(lines)} lines")
# loop over lines
# for line in lines....
Alternatively, you can use check_output(), which will convert to a str itself.

import subprocess

cmd = "/bin/ls"
output = subprocess.check_output(cmd)
lines = output.splitlines()
print(f"{cmd} output contained {len(lines)} lines")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,075 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to combine two output in one variable? ilknurg 2 1,184 Aug-01-2022, 09:31 AM
Last Post: Gribouillis
  How to write a part of powershell command as a variable? ilknurg 2 1,118 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  Os command output in variable shows wrong value paulo79 2 1,503 Apr-09-2022, 03:48 PM
Last Post: ndc85430
  How to use a variable in linux command in python code? ilknurg 2 1,596 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  How to input & output parameters from command line argument shantanu97 1 2,552 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Getting a GET request output text into a variable to work with it. LeoT 2 2,985 Feb-24-2021, 02:05 PM
Last Post: LeoT
  can we write command output to new csv file using Panda package? PythonBeginner_2020 3 2,372 Mar-13-2020, 12:38 PM
Last Post: ndc85430
  Correct syntax for a variable inside a quotes: MP4Box command JonnyDriller 2 2,750 Feb-02-2020, 01:22 AM
Last Post: JonnyDriller
  Capture grep output to a variable which includes another variable kdefilip2 4 8,195 Nov-24-2019, 12:30 PM
Last Post: kdefilip2

Forum Jump:

User Panel Messages

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