Aug-30-2017, 01:45 PM
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 :
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
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