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
  Insert command line in script lif 4 1,023 Mar-24-2025, 10:30 PM
Last Post: lif
  Simplest way to run external command line app with parameters? Winfried 2 1,287 Aug-19-2024, 03:11 PM
Last Post: snippsat
  Is possible to run the python command to call python script on linux? cuten222 6 2,394 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Command line argument issue space issue mg24 5 2,353 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  accept command line argument mg24 5 2,371 Sep-27-2022, 05:58 PM
Last Post: snippsat
  How to use a variable in linux command in python code? ilknurg 2 2,422 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  use subprocess on linux\pi wwith a "grep " command korenron 2 11,262 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  Accessing varying command line arguements Rakshan 3 3,013 Jul-28-2021, 03:18 PM
Last Post: snippsat
  How to input & output parameters from command line argument shantanu97 1 3,626 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  how to run linux command with multi pipes by python !! evilcode1 2 8,950 Jan-25-2021, 11:19 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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