how to parse system functions output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • praba kar

    #1

    how to parse system functions output

    Dear all,

    I want to parse the system functions output
    but I couldn't do it. Kindly assist me in this
    task.

    eg) bytesused = os.system('du -sh /Users/enmail')
    if I print this bytesused variable the output of
    bytesused variable is the below

    14M /Users/enmail
    0

    Now From this Output I want only '14M" I cannot split
    this output by space. If anyone know regarding this
    mail me ASAP

    regards
    Prabahar


    _______________ _______________ _______________ _______________ ____________
    Yahoo! India Matrimony: Find your life partner online
    Go to: http://yahoo.shaadi.com/india-matrimony
  • Peter Hansen

    #2
    Re: how to parse system functions output

    praba kar wrote:[color=blue]
    > eg) bytesused = os.system('du -sh /Users/enmail')
    > if I print this bytesused variable the output of
    > bytesused variable is the below
    >
    > 14M /Users/enmail
    > 0[/color]

    It's unlikely this is the contents of "bytesused" ,
    because os.system() does not return the output
    of the program that is run, it returns only the
    exit value. (See the doc.) The "14M ..." part
    is actually being printed by the "du" command
    as it runs. "bytesused" contains only the "0"
    that you show above.
    [color=blue]
    > Now From this Output I want only '14M" I cannot split
    > this output by space. If anyone know regarding this
    > mail me ASAP[/color]

    You are looking for either os.popen() or the newer,
    more flexible "subprocess " module (available in
    Python 2.4). Again, check the docs for details and
    examples for both of these.

    Basically you would do this:

    result = os.popen('du -sh /Users/enmail').read()
    # now parse result...

    -Peter

    Comment

    • wittempj@hotmail.com

      #3
      Re: how to parse system functions output

      Or if you run 2.4 you can use subprocess

      Comment

      Working...