Python Forum
Saving shell command into a List
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving shell command into a List
#1
Hello all!

Kindly find below what I'm trying to do and what is my issue.

Goal :
Execute shell command "ls -l" and save the result into a list.

Issue :
Executing shell command -- OK
Save it into a list -- KO

Other infos :
OS used : Fedora release 26 (Twenty Six)
Python version : Python 3.6.2


Code :
#!/usr/bin/python3

#########################################
#                                       #
#                                       #
#       Importation des librairies      #
#                                       #
#                                       #
#########################################

import subprocess

#########################################
#					#
#					#
#	Déclaration des variables	#
#					#
#					#
#########################################

resultat = ""

#########################################
#                                       #
#                                       #
#       	Programme 	        #
#                                       #
#					#
#########################################


#resultat = subprocess.call(["ls", "-l", "/var/log"])
#resultat = subprocess.Popen(["ls", "-l", "/var/log"], stdout=subprocess.PIPE)
#subprocess.run(["ls", "-l", "/var"], stdout=subprocess.PIPE)
p = subprocess.Popen(["ls", "-l", "/var/log"], stdout=subprocess.PIPE)
#(output, err) = p.communicate()
p_status = p.wait()
for line in p.stdout:
  print (line)

#print ("Command output : ", output)
#output, err = resultat.communicate()


#resultat = subprocess.call(["ls", "-l", "/var/log"])

#print(resultat)
#print(type(resultat))
I am really not familiar with subprocess library nor with Popen constructor and I don't understand them.
I know I should use Popen construtor (import subprocess) and I tried to read several documentation.
Unfortunately, my technical english isn't that good, I couldn't understand them.

Also, my code is kinda random.
As you can see I have tried several method to get a proper print because I didn't really understand how to use the library subprocess.

Could you help me with saving my 'ls -l' command into a list?

Thanks!

Cheers,

Steackfrite
Reply
#2
In the Linux OS, It's already in a list called history.
Reply
#3
(Aug-30-2017, 01:45 PM)steackfrite Wrote: Could you help me with saving my 'ls -l' command into a list?
Use check_output().
It will capture output as bytes(Python 3),so we convert to string bye using decode('utf-8') or just decode().
Then it will be a string(str) in Python 3,split bye \n to make into a list.
Example:
from subprocess import check_output

out = check_output(['ls', '-l', '/home/mint/Downloads'])
result = out.decode('utf-8').strip()
print(result)
# Make it a list
print(result.split('\n'))
Output:
mint@mint ~ $ python sub_ls.py total 4 drwxr-xr-x 9 mint mint 1000 Aug 12 17:54 firefox -rw-rw-r-- 1 mint mint    2 Aug 30 14:22 test ['total 4', 'drwxr-xr-x 9 mint mint 1000 Aug 12 17:54 firefox', '-rw-rw-r-- 1 mint mint    2 Aug 30 14:22 test']
Reply
#4
Thank you snippsat!

That is exactly what I wanted to do.

I'll try it tomorrow.

Cheers!
Reply
#5
Confirmed this is exactly what I needed.

Thanks a lot!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open a dos shell for ssh command martinmistere 6 1,809 Jun-07-2022, 12:24 PM
Last Post: martinmistere
  Saving list in a list quest_ 3 3,165 Mar-10-2021, 09:58 AM
Last Post: quest_
  Sound Approach for Running a Shell Command? matt_the_hall 8 3,431 Dec-14-2020, 02:52 PM
Last Post: matt_the_hall
  Passing List of Objects in Command Line Python usman 7 3,247 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  Select correct item from list for subprocess command pythonnewbie138 6 3,359 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  python if command for a list fid 3 2,035 May-08-2020, 11:52 AM
Last Post: fid
  Add list item into executable command zhome888 2 2,155 Dec-19-2019, 12:23 AM
Last Post: zhome888
  How to use list (structure) in the SQL Select command? pyuser1 2 2,950 Apr-27-2018, 02:33 PM
Last Post: pyuser1
  ssh remote command with list pythonnew 2 4,637 Jun-17-2017, 12:46 AM
Last Post: pythonnew

Forum Jump:

User Panel Messages

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