473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Getting directory size

How can we get a full directory size (sum of all his data)?
like when we type `du -sh mydir`

Because os.path.getsize('mydir') only give the size of the directory physical representation on the disk.

Thanks
Jul 18 '05 #1
4 21372
francisl wrote:
How can we get a full directory size (sum of all his data)?
like when we type `du -sh mydir`

Because os.path.getsize('mydir') only give the size of the directory
physical representation on the disk.


os.popen('du -sh mydir') would be one approach.

The harder way is to use os.walk('mydir') to scan all
files in all subdirectories, and use os.stat() or
os.path.getsize() to add up the sizes of each file.

-Peter
Jul 18 '05 #2
Peter Hansen wrote:
francisl wrote:
How can we get a full directory size (sum of all his data)?
like when we type `du -sh mydir`

Because os.path.getsize('mydir') only give the size of the directory physical representation on the disk.


os.popen('du -sh mydir') would be one approach.

The harder way is to use os.walk('mydir') to scan all
files in all subdirectories, and use os.stat() or
os.path.getsize() to add up the sizes of each file.


With Orendorff's path module, this becomes a two-liner:

from path import path
dir_size = sum([f.size for f in path('mydir').walkfiles()])

With Python 2.4 genexps, you don't even need the square brackets...

http://www.jorendorff.com/articles/python/path/

Shoulda-been-added-to-the-standard-library'ly yours,

-- Graham

Jul 18 '05 #3
P
francisl wrote:
How can we get a full directory size (sum of all his data)?
like when we type `du -sh mydir`

Because os.path.getsize('mydir') only give the size of the directory
physical representation on the disk.


Have a look at:
http://www.pixelbeat.org/scripts/dutop

Pádraig.
Jul 18 '05 #4
Graham Fawcett wrote:
Peter Hansen wrote:
francisl wrote:
How can we get a full directory size (sum of all his data)?
like when we type `du -sh mydir`

Because os.path.getsize('mydir') only give the size of the
directory
physical representation on the disk.


os.popen('du -sh mydir') would be one approach.

The harder way is to use os.walk('mydir') to scan all
files in all subdirectories, and use os.stat() or
os.path.getsize() to add up the sizes of each file.

With Orendorff's path module, this becomes a two-liner:

from path import path
dir_size = sum([f.size for f in path('mydir').walkfiles()])

With Python 2.4 genexps, you don't even need the square brackets...

http://www.jorendorff.com/articles/python/path/

Shoulda-been-added-to-the-standard-library'ly yours,

-- Graham


Thank you all

Before I received any anwser, I wrote a recursive function that walk into directory adding file size with os.getsize().
But I was stop because the file have an average of 15 go. And the object return that the size is too large for valid assignation.

I have to use 1.5.2 at work, because it is the only version available on our vmware server.

So I thing I will have to try popen.
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nate Harel | last post by:
Hi all, I need to write a small script that gives me the size of a particular directory. Is there a command or set of commands that will do this? thanks Nate
1
by: Carolyn Speakman | last post by:
Hi, I need to get a directory listing from a client machine. The directory will be specified by the user and the results will be stored on the server in an xml file. I've been trying to use the...
0
by: PlanarIndia | last post by:
hi, I am facing an issue with getting the file size of a network shared file. I tried using FileInfo.length and it is returning me size that is rounded off to the nearest 1024 bytes. E.g. for...
17
by: Arnold | last post by:
Is using fseek and ftell a reliable method of getting the file size on a binary file? I thought I remember reading somewhere it wasn't... If not what would be the "right" and portable method to...
11
by: KarimL | last post by:
Thanks for your advices... but i need to get the Image height because i dynamically resize the height of my webcontrol based on the image height. More i just have the url (relative parth) to the...
2
by: Raed Sawalha | last post by:
I have the following function to get the directory sizse, i wondering if there any better way to find directory size public static double GetDirectorySize(string DirectoryPath) { double...
4
by: Von Thep via DotNetMonster.com | last post by:
How can I use WMI with VB.NET to get a remote computers directory size? How can I use a unc_path within WMI? Previously I used FileInfo and DirectoryInfo but this method takes too long because...
2
by: mail2ganeshguru | last post by:
Friends, i want to calculate the size of the folders. Please give me some suggestion in this. thanks Ganesh.G
1
Ganon11
by: Ganon11 | last post by:
Hey All, For fun, I'm trying to write a small script to determine where the majority of my space is being taken up. Unfortunately, on Windows, when I use the -s function on a directory, it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.