Python Forum

Full Version: Saving shell command into a List
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
In the Linux OS, It's already in a list called history.
(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']
Thank you snippsat!

That is exactly what I wanted to do.

I'll try it tomorrow.

Cheers!
Confirmed this is exactly what I needed.

Thanks a lot!