Python Forum
How to get from linux command line to an array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get from linux command line to an array
#1
Hello, I am a totally newby.

I am trying to convert from Linux Bash to python. 

I need the output of Linux commands to be stored in a Python array.


######################################
PythonArray <   ls -lar
 
for X in (PythonArray):
    print X
######################################


this is not real code as you know however I hope it explains what I need. 

If I am not clear, feel free to ask me to clarify
Reply
#2
Working with processes tends to be kind of a pain. Do you actually want a list ("Python array") of the lines of a Bash command, or is that an implementation detail for something else you actually care about? Writing it in pure Python without messing with Bash would probably be better.
Reply
#3
(Dec-04-2016, 11:51 PM)timfox123 Wrote: Hello, I am a totally newby.

I am trying to convert from Linux Bash to python. 

I need the output of Linux commands to be stored in a Python array.


######################################
PythonArray <   ls -lar
 
for X in (PythonArray):
    print X
######################################


this is not real code as you know however I hope it explains what I need. 

If I am not clear, feel free to ask me to clarify

Python 3
import subprocess

def main():
    process = subprocess.Popen(['ls', '-lar'], stdout=subprocess.PIPE)
    out, err = process.communicate()
    #print(out)
    # decode bytes to python string
    out = out.decode('utf-8')
    #print(out)
    # convert to python list
    out = out.split('\n')
    #print(out)
    for o in out:
        print(o)    

if __name__ == "__main__":
    main()
99 percent of computer problems exists between chair and keyboard.
Reply
#4
There are also python commands to do things like "ls" much easier, if you intend to parse the outputs, etc. Welcome to python! :)
Reply
#5
Thank you all !!!!

this will do it
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 630 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Command line argument issue space issue mg24 5 1,279 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  accept command line argument mg24 5 1,239 Sep-27-2022, 05:58 PM
Last Post: snippsat
  How to use a variable in linux command in python code? ilknurg 2 1,547 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  use subprocess on linux\pi wwith a "grep " command korenron 2 7,902 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  Accessing varying command line arguements Rakshan 3 2,008 Jul-28-2021, 03:18 PM
Last Post: snippsat
  How to input & output parameters from command line argument shantanu97 1 2,505 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  how to run linux command with multi pipes by python !! evilcode1 2 6,217 Jan-25-2021, 11:19 AM
Last Post: DeaD_EyE
  Passing List of Objects in Command Line Python usman 7 3,085 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  create an array of each line of text macieju1974 7 3,353 Jun-07-2020, 06:30 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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