![]() |
Saving shell command into a List - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Saving shell command into a List (/thread-4622.html) |
Saving shell command into a List - steackfrite - Aug-30-2017 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 RE: Saving shell command into a List - Larz60+ - Aug-30-2017 In the Linux OS, It's already in a list called history. RE: Saving shell command into a List - snippsat - Aug-30-2017 (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'))
RE: Saving shell command into a List - steackfrite - Aug-30-2017 Thank you snippsat! That is exactly what I wanted to do. I'll try it tomorrow. Cheers! RE: Saving shell command into a List - steackfrite - Aug-31-2017 Confirmed this is exactly what I needed. Thanks a lot! |