capturing output of command line in an array

Discussion in 'Python' started by amjadcsu@gmail.com, Oct 31, 2007.

  1. Guest

    Hi

    I am trying to execute a command using os.system. this command lists
    the number of nodes alive in a cluster. I would like to capture the
    output in list/array in python. IS it possible.?/

    Here is my command
    gstat -a
    node13 2 ( 0/ 56) [ 0.00, 0.00, 0.00] [ 0.0, 0.0,
    0.1, 99.9, 0.0] OFF
    node12 2 ( 1/ 63) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    3.7, 49.8, 0.0] OFF
    node8 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 47.5, 0.0,
    2.8, 49.7, 0.0] OFF
    node2 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    3.7, 49.7, 0.0] OFF
    node1 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    3.7, 49.7, 0.0] OFF
    node7 2 ( 1/ 58) [ 0.99, 0.97, 0.91] [ 49.8, 0.0,
    0.6, 49.7, 0.0] OFF
    node11 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    3.8, 49.6, 0.0] OFF
    node4 2 ( 1/ 59) [ 1.00, 1.00, 0.93] [ 46.6, 0.0,
    3.7, 49.7, 0.0] OFF
    node10 2 ( 1/ 59) [ 1.00, 1.00, 0.94] [ 46.7, 0.0,
    3.7, 49.7, 0.0] OFF
    node5 2 ( 1/ 59) [ 1.00, 1.00, 0.92] [ 49.8, 0.0,
    0.4, 49.7, 0.0] OFF
    node6 2 ( 1/ 59) [ 1.00, 1.00, 0.94] [ 49.5, 0.0,
    0.9, 49.7, 0.0] OFF

    I would like to have an array where each element contains the nodes

    for example
    a=[node13,node12,node2,node8,node1,node7,node 11]

    any help would be appreciated

    thanks
     
    , Oct 31, 2007
    #1
    1. Advertising

  2. Lee Capps Guest

    On Oct 31, 2007, at 11:01 AM, wrote:

    > I am trying to execute a command using os.system. this command lists
    > the number of nodes alive in a cluster. I would like to capture the
    > output in list/array in python. IS it possible.?/
    >
    > Here is my command
    > gstat -a
    > node13 2 ( 0/ 56) [ 0.00, 0.00, 0.00] [ 0.0, 0.0,
    > 0.1, 99.9, 0.0] OFF
    > node12 2 ( 1/ 63) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > 3.7, 49.8, 0.0] OFF
    > node8 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 47.5, 0.0,
    > 2.8, 49.7, 0.0] OFF
    > node2 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > 3.7, 49.7, 0.0] OFF
    > node1 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > 3.7, 49.7, 0.0] OFF
    > node7 2 ( 1/ 58) [ 0.99, 0.97, 0.91] [ 49.8, 0.0,
    > 0.6, 49.7, 0.0] OFF
    > node11 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > 3.8, 49.6, 0.0] OFF
    > node4 2 ( 1/ 59) [ 1.00, 1.00, 0.93] [ 46.6, 0.0,
    > 3.7, 49.7, 0.0] OFF
    > node10 2 ( 1/ 59) [ 1.00, 1.00, 0.94] [ 46.7, 0.0,
    > 3.7, 49.7, 0.0] OFF
    > node5 2 ( 1/ 59) [ 1.00, 1.00, 0.92] [ 49.8, 0.0,
    > 0.4, 49.7, 0.0] OFF
    > node6 2 ( 1/ 59) [ 1.00, 1.00, 0.94] [ 49.5, 0.0,
    > 0.9, 49.7, 0.0] OFF
    >
    > I would like to have an array where each element contains the nodes
    >
    > for example
    > a=[node13,node12,node2,node8,node1,node7,node 11]
    >
    > any help would be appreciated
    >


    You might try os.popen to read in the output of your command.

    It would be helpful to have more information about what you'd like to
    do with your list. Do you just want to put the text of the
    individual node lines from gstat into the list? If so, os.popen
    should work for you. Or do you need to parse the lines and store
    them in, say dictionaries? Tuples? If you need to parse it, you
    could use module re, or even just string methods, depending on what
    you're trying to do . . . .

    HTH,

    ---
    Lee Capps
    Technology Specialist
    CTE Resource Center
     
    Lee Capps, Oct 31, 2007
    #2
    1. Advertising

  3. Guest

    Thanks Lee
    I am looking to just get the node name from that info.
    I dont need all other info
    so my list would be just
    alist=[node13,node12,node8,node1 ....]
    is it possible??

    On Oct 31, 9:44 am, Lee Capps <> wrote:
    > On Oct 31, 2007, at 11:01 AM, wrote:
    >
    >
    >
    > > I am trying to execute a command using os.system. this command lists
    > > the number of nodes alive in a cluster. I would like to capture the
    > > output in list/array in python. IS it possible.?/

    >
    > > Here is my command
    > > gstat -a
    > > node13 2 ( 0/ 56) [ 0.00, 0.00, 0.00] [ 0.0, 0.0,
    > > 0.1, 99.9, 0.0] OFF
    > > node12 2 ( 1/ 63) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > > 3.7, 49.8, 0.0] OFF
    > > node8 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 47.5, 0.0,
    > > 2.8, 49.7, 0.0] OFF
    > > node2 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > > 3.7, 49.7, 0.0] OFF
    > > node1 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > > 3.7, 49.7, 0.0] OFF
    > > node7 2 ( 1/ 58) [ 0.99, 0.97, 0.91] [ 49.8, 0.0,
    > > 0.6, 49.7, 0.0] OFF
    > > node11 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > > 3.8, 49.6, 0.0] OFF
    > > node4 2 ( 1/ 59) [ 1.00, 1.00, 0.93] [ 46.6, 0.0,
    > > 3.7, 49.7, 0.0] OFF
    > > node10 2 ( 1/ 59) [ 1.00, 1.00, 0.94] [ 46.7, 0.0,
    > > 3.7, 49.7, 0.0] OFF
    > > node5 2 ( 1/ 59) [ 1.00, 1.00, 0.92] [ 49.8, 0.0,
    > > 0.4, 49.7, 0.0] OFF
    > > node6 2 ( 1/ 59) [ 1.00, 1.00, 0.94] [ 49.5, 0.0,
    > > 0.9, 49.7, 0.0] OFF

    >
    > > I would like to have an array where each element contains the nodes

    >
    > > for example
    > > a=[node13,node12,node2,node8,node1,node7,node 11]

    >
    > > any help would be appreciated

    >
    > You might try os.popen to read in the output of your command.
    >
    > It would be helpful to have more information about what you'd like to
    > do with your list. Do you just want to put the text of the
    > individual node lines from gstat into the list? If so, os.popen
    > should work for you. Or do you need to parse the lines and store
    > them in, say dictionaries? Tuples? If you need to parse it, you
    > could use module re, or even just string methods, depending on what
    > you're trying to do . . . .
    >
    > HTH,
    >
    > ---
    > Lee Capps
    > Technology Specialist
    > CTE Resource Center
    >
     
    , Oct 31, 2007
    #3
  4. En Wed, 31 Oct 2007 19:20:56 -0300, <> escribiรณ:

    > I am looking to just get the node name from that info.
    > I dont need all other info
    > so my list would be just
    > alist=[node13,node12,node8,node1 ....]
    > is it possible??
    >
    >> > node13 2 ( 0/ 56) [ 0.00, 0.00, 0.00] [ 0.0, 0.0,
    >> > 0.1, 99.9, 0.0] OFF
    >> > node12 2 ( 1/ 63) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    >> > 3.7, 49.8, 0.0] OFF
    >> > node8 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 47.5, 0.0,
    >> > 2.8, 49.7, 0.0] OFF


    Try this:

    import subprocess
    p = subprocess.Popen( ["your","command","+args"], stdout=subprocess.PIPE)
    lines = p.stdout.readlines()
    p.wait()

    If you are only interested in the first word in each line, you may replace
    the lines = ... above with this:

    nodes = [line.split(' ', 1)[0] for line in p.stdout]

    --
    Gabriel Genellina
     
    Gabriel Genellina, Nov 1, 2007
    #4
  5. Guest

    thanks Lee,
    I would just like to get the "node" in that list.
    so it would be something like this
    alist=[node13,node12,node8,node2,node1.....]
    I dont want the whole info
    is it possible??
    On Oct 31, 9:44 am, Lee Capps <> wrote:
    > On Oct 31, 2007, at 11:01 AM, wrote:
    >
    >
    >
    > > I am trying to execute a command using os.system. this command lists
    > > the number of nodes alive in a cluster. I would like to capture the
    > > output in list/array in python. IS it possible.?/

    >
    > > Here is my command
    > > gstat -a
    > > node13 2 ( 0/ 56) [ 0.00, 0.00, 0.00] [ 0.0, 0.0,
    > > 0.1, 99.9, 0.0] OFF
    > > node12 2 ( 1/ 63) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > > 3.7, 49.8, 0.0] OFF
    > > node8 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 47.5, 0.0,
    > > 2.8, 49.7, 0.0] OFF
    > > node2 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > > 3.7, 49.7, 0.0] OFF
    > > node1 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > > 3.7, 49.7, 0.0] OFF
    > > node7 2 ( 1/ 58) [ 0.99, 0.97, 0.91] [ 49.8, 0.0,
    > > 0.6, 49.7, 0.0] OFF
    > > node11 2 ( 1/ 59) [ 0.99, 0.97, 0.91] [ 46.6, 0.0,
    > > 3.8, 49.6, 0.0] OFF
    > > node4 2 ( 1/ 59) [ 1.00, 1.00, 0.93] [ 46.6, 0.0,
    > > 3.7, 49.7, 0.0] OFF
    > > node10 2 ( 1/ 59) [ 1.00, 1.00, 0.94] [ 46.7, 0.0,
    > > 3.7, 49.7, 0.0] OFF
    > > node5 2 ( 1/ 59) [ 1.00, 1.00, 0.92] [ 49.8, 0.0,
    > > 0.4, 49.7, 0.0] OFF
    > > node6 2 ( 1/ 59) [ 1.00, 1.00, 0.94] [ 49.5, 0.0,
    > > 0.9, 49.7, 0.0] OFF

    >
    > > I would like to have an array where each element contains the nodes

    >
    > > for example
    > > a=[node13,node12,node2,node8,node1,node7,node 11]

    >
    > > any help would be appreciated

    >
    > You might try os.popen to read in the output of your command.
    >
    > It would be helpful to have more information about what you'd like to
    > do with your list. Do you just want to put the text of the
    > individual node lines from gstat into the list? If so, os.popen
    > should work for you. Or do you need to parse the lines and store
    > them in, say dictionaries? Tuples? If you need to parse it, you
    > could use module re, or even just string methods, depending on what
    > you're trying to do . . . .
    >
    > HTH,
    >
    > ---
    > Lee Capps
    > Technology Specialist
    > CTE Resource Center
    >
     
    , Nov 1, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aditya Mahajan
    Replies:
    4
    Views:
    108
    mortee
    Oct 15, 2007
  2. Kyle Schmitt
    Replies:
    9
    Views:
    150
    Pablo Q.
    Aug 6, 2008
  3. Dib Urim
    Replies:
    4
    Views:
    209
    Ben Liddicott
    Nov 23, 2003
  4. Dib Urim
    Replies:
    4
    Views:
    214
    Michele Dondi
    Dec 3, 2003
  5. Marek Stepanek
    Replies:
    12
    Views:
    438
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page