Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > get output of du / ls command - currently ugly code ...

Reply
Thread Tools

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

 
 
Esmail
Guest
Posts: n/a
 
      08-13-2009
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

 
Reply With Quote
 
 
 
 
rebfj@yahoo.co.uk
Guest
Posts: n/a
 
      08-13-2009
On Aug 13, 8:28*pm, Esmail <(E-Mail Removed)> 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)
 
Reply With Quote
 
 
 
 
Esmail
Guest
Posts: n/a
 
      08-13-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Aug 13, 8:28 pm, Esmail <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Slightly ugly output rf HTML 25 08-06-2010 08:16 AM
Re: Slightly ugly output Denis McMahon HTML 2 08-02-2010 01:40 PM
Java SAX parser. How to get the raw XML code of the currently parsingevent PatlaDJ XML 4 07-03-2008 07:39 PM
The UGLY output from P&S superzooms Rich Digital Photography 34 12-23-2007 09:30 AM
Hi I have one web application and i want to get the number of users who are currently accessing the application. Also I want to get the user details of each user, which is stored in a database. How can I do this? Pls help. Getting No: and anu Java 11 05-12-2005 03:25 PM



Advertisments