Newbie question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken D'Ambrosio

    Newbie question...

    First, apologies for such a newbie question; if there's a better forum
    (I've poked around, some) feel free to point it out to me. Anyway, a
    mere 25-odd years after first hearing about OOP, I've finally decided to
    go to it, by way of Python. But this puzzles me:

    import commands
    free = commands.getout put("free")
    for line in free:
    print line,

    Gives:
    t o t a l u s e d
    f r e e
    s h a r e d b u f f e r s c a c h e d
    M e m : 5 1 5 9 9 2 4 6 0 4 5 2 5
    5 5 4 0
    0 7 7 5 1 6 9 1 8 8 4
    - / + b u f f e r s / c a c h e : 2 9 1 0 5 2 2 2
    4 9 4 0

    Why are there spaces between everything? And how do I keep it from
    happening? *confused*

    Thanks much,

    -Ken
    ** Posted from http://www.teranews.com **
  • Tim Leslie

    #2
    Re: Newbie question...

    On Tue, Sep 30, 2008 at 12:04 PM, Ken D'Ambrosio <ken@jots.orgwr ote:
    First, apologies for such a newbie question; if there's a better forum (I've
    poked around, some) feel free to point it out to me. Anyway, a mere 25-odd
    years after first hearing about OOP, I've finally decided to go to it, by
    way of Python. But this puzzles me:
    >
    import commands
    free = commands.getout put("free")
    # free is now a string, representing the output from the "free" command

    for line in free:
    print line,

    This isn't doing what you think. Since free is a string, when you
    iterate over it, you get a single character each time, so your line
    variable isn't actually a line of the output, but a single character.
    When you run "print line," this prints the character, followed by a
    space. The comma at the end of the print statement tells it to put a
    space after the output rather than a newline.

    What you probably wanted to do is split up your output on the newline character.

    for line in free.split("\n" ):
    print line

    Of course, your string already has all the newlines it needs in it, so
    if all you want is to see the output of the "free" command you can
    just do:

    print free

    HTH,

    Tim
    >
    Gives:
    t o t a l u s e d f r e e
    s h a r e d b u f f e r s c a c h e d
    M e m : 5 1 5 9 9 2 4 6 0 4 5 2 5 5 5
    4 0
    0 7 7 5 1 6 9 1 8 8 4
    - / + b u f f e r s / c a c h e : 2 9 1 0 5 2 2 2 4 9
    4 0
    >
    Why are there spaces between everything? And how do I keep it from
    happening? *confused*
    >
    Thanks much,
    >
    -Ken
    ** Posted from http://www.teranews.com **
    --

    >

    Comment

    • Ken Seehart

      #3
      Re: Newbie question...

      Ken D'Ambrosio wrote:
      First, apologies for such a newbie question; if there's a better forum
      (I've poked around, some) feel free to point it out to me. Anyway, a
      mere 25-odd years after first hearing about OOP, I've finally decided
      to go to it, by way of Python. But this puzzles me:
      >
      import commands
      free = commands.getout put("free")
      for line in free:
      print line,
      >
      Gives:
      t o t a l u s e d f r e e
      s h a r e d b u f f e r s c a c h e d
      M e m : 5 1 5 9 9 2 4 6 0 4 5 2
      5 5 5 4 0
      0 7 7 5 1 6 9 1 8 8 4
      - / + b u f f e r s / c a c h e : 2 9 1 0 5 2 2
      2 4 9 4 0
      >
      Why are there spaces between everything? And how do I keep it from
      happening? *confused*
      >
      Thanks much,
      >
      -Ken
      ** Posted from http://www.teranews.com **
      --

      >
      The variable 'free' is a string containing all of the output, not a file
      object or a sequence of strings. Therefore, when you iterate free you
      iterate a sequence of characters. This is different than the case of
      iterating an open file, which would give you a sequence of lines as you
      expect.

      So ...

      print line,

      .... prints each character followed by a space and no newline.

      You can do this instead:

      import commands
      free = commands.getout put("free")
      print free


      - Ken (that's my name too)

      Comment

      Working...