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


Messages In This Thread
Saving shell command into a List - by steackfrite - Aug-30-2017, 01:45 PM
RE: Saving shell command into a List - by Larz60+ - Aug-30-2017, 02:28 PM
RE: Saving shell command into a List - by snippsat - Aug-30-2017, 03:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Open a dos shell for ssh command martinmistere 6 1,943 Jun-07-2022, 12:24 PM
Last Post: martinmistere
  Saving list in a list quest_ 3 3,262 Mar-10-2021, 09:58 AM
Last Post: quest_
  Sound Approach for Running a Shell Command? matt_the_hall 8 3,621 Dec-14-2020, 02:52 PM
Last Post: matt_the_hall
  Passing List of Objects in Command Line Python usman 7 3,424 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  Select correct item from list for subprocess command pythonnewbie138 6 3,556 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  python if command for a list fid 3 2,158 May-08-2020, 11:52 AM
Last Post: fid
  Add list item into executable command zhome888 2 2,239 Dec-19-2019, 12:23 AM
Last Post: zhome888
  How to use list (structure) in the SQL Select command? pyuser1 2 3,076 Apr-27-2018, 02:33 PM
Last Post: pyuser1
  ssh remote command with list pythonnew 2 4,737 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