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

Esmail ebonak at
Thu Aug 13 22:13:51 CEST 2009

rebfj at wrote:
> On Aug 13, 8:28 pm, Esmail <ebo... at> 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


  > 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,


More information about the Python-list mailing list