ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/directorySize2.py
Revision: 8
Committed: Sat May 5 04:21:19 2012 UTC (13 years, 10 months ago) by ninoborges
Content type: text/x-python
File size: 1737 byte(s)
Log Message:
Initial Import

File Contents

# User Rev Content
1 ninoborges 8 import os
2    
3     def get_dir_size(root, rawSize = False):
4     size = 0
5     for path, dirs, files in os.walk(root):
6     for f in files:
7     size += os.path.getsize( os.path.join( path, f ) )
8     prettySize = pretty_filesize(size)
9     if rawSize:
10     return size
11     else:
12     return prettySize
13    
14    
15     def pretty_filesize(bytes):
16     if bytes >= 1073741824:
17     return str(bytes / 1024 / 1024 / 1024) + ' GB'
18     elif bytes >= 1048576:
19     return str(bytes / 1024 / 1024) + ' MB'
20     elif bytes >= 1024:
21     return str(bytes / 1024) + ' KB'
22     elif bytes < 1024:
23     return str(bytes) + ' bytes'
24    
25     def pretty_filesize2(bytes):
26     """ Experimental version that will support floating point calculations"""
27     if bytes >= 1073741824:
28     prettySize = str(bytes / 1024. / 1024. / 1024.) + ' GB'
29     elif bytes >= 1048576:
30     prettySize = str(bytes / 1024. / 1024.) + ' MB'
31     elif bytes >= 1024:
32     prettySize = str(bytes / 1024.) + ' KB'
33     elif bytes < 1024:
34     prettySize = str(bytes) + ' bytes'
35     if "." in prettySize:
36     prettySize = prettySize[:prettySize.index('.')+3] +prettySize [-3:]
37     return prettySize
38    
39     def prettyToUnpretty_Filesize(prettySize):
40     """This will convert from a pretty size above back to the bytes value"""
41     if 'GB' in prettySize:
42     size = prettySize.replace(' GB','')
43     bytes = float(size) * 1024. * 1024. * 1024.
44     elif 'MB' in prettySize:
45     size = prettySize.replace(' MB','')
46     bytes = float(size) * 1024. * 1024.
47     elif 'KB' in prettySize:
48     size = prettySize.replace(' KB','')
49     bytes = float(size) * 1024.
50     else:
51     bytes = None
52     return bytes