get output of du / ls command - currently ugly code ...

Discussion in 'Python' started by Esmail, Aug 13, 2009.

  1. Esmail

    Esmail Guest

    Hi all,

    Essentially all I want to know the size of a directory, and the size
    of a zipped tarball so that I can compute/report the compression ratio.

    The code I have seems hideous, but it seems to work. Surely there is an
    easier,more elegant way to do this?

    dir_size = os.popen('du -sk somename')
    data = dir_size.readlines()
    dir_size = int(data[0].split()[0])
    print 'dir size: ', dir_size

    tar_size = os.popen('ls -s somename.tar.gz')
    data = tar_size.readlines()
    tar_size = int(data[0].split()[0])
    print 'tar size: ', tar_size

    Thanks
    Esmail
     
    Esmail, Aug 13, 2009
    #1
    1. Advertising

  2. Esmail

    Guest

    On Aug 13, 8:28 pm, Esmail <> wrote:
    > Hi all,
    >
    > Essentially all I want to know the size of a directory, and the size
    > of a zipped tarball so that I can compute/report the compression ratio.
    >
    > The code I have seems hideous, but it seems to work. Surely there is an
    > easier,more elegant way to do this?
    >
    >      dir_size = os.popen('du -sk somename')
    >      data = dir_size.readlines()
    >      dir_size = int(data[0].split()[0])
    >      print 'dir size: ', dir_size
    >
    >      tar_size = os.popen('ls -s somename.tar.gz')
    >      data = tar_size.readlines()
    >      tar_size = int(data[0].split()[0])
    >      print 'tar size: ', tar_size
    >
    > Thanks
    > Esmail


    Try using os.path.getsize(somename.tar.gz)
     
    , Aug 13, 2009
    #2
    1. Advertising

  3. Esmail

    Esmail Guest

    wrote:
    > On Aug 13, 8:28 pm, Esmail <> wrote:
    >> Hi all,
    >>
    >> Essentially all I want to know the size of a directory, and the size
    >> of a zipped tarball so that I can compute/report the compression ratio.
    >>


    >> dir_size = os.popen('du -sk somename')
    >> data = dir_size.readlines()
    >> dir_size = int(data[0].split()[0])
    >> print 'dir size: ', dir_size
    >>


    Hi,

    > Try using os.path.getsize(somename.tar.gz)


    Beautiful! .. that takes care of the tarball .. but it doesn't seem to
    work for reporting the total size of the directory. Any suggestions how
    to do that in a nicer way?

    I just looked through os.path.* and there doesn't seem to be a function
    to report what "du -sb" would.

    Thanks for your help,

    Esmail
     
    Esmail, Aug 13, 2009
    #3
    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. PatlaDJ
    Replies:
    4
    Views:
    917
    PatlaDJ
    Jul 3, 2008
  2. rf
    Replies:
    25
    Views:
    1,087
    Neredbojias
    Aug 6, 2010
  3. Denis McMahon

    Re: Slightly ugly output

    Denis McMahon, Aug 2, 2010, in forum: HTML
    Replies:
    2
    Views:
    429
    Denis McMahon
    Aug 2, 2010
  4. Andy

    Re: Slightly ugly output

    Andy, Aug 2, 2010, in forum: HTML
    Replies:
    11
    Views:
    776
    Neredbojias
    Aug 4, 2010
  5. Jason Stacy
    Replies:
    1
    Views:
    118
    Jürgen Exner
    May 4, 2008
Loading...

Share This Page