Get actual disk space


Get actual disk space



How do I get the actual filesize on disk in python? (the actual size it takes on the harddrive).



Thanks.





You mean rouned up by cluster size?
– ruslik
Nov 25 '10 at 8:18






Take a look at this question: stackoverflow.com/questions/2493172/…
– Ruel
Nov 25 '10 at 8:24





@ruslik: It's not that simple. Consider e.g. sparse or compressed files, which can take less space than their size indicates.
– Philipp
Nov 25 '10 at 9:13




5 Answers
5


st = os.stat(…)
du = st.st_blocks * st.st_blksize





+1, didn't realise this was in os.stat! I was about to refer the questioner to win32file.DeviceIoControl. Don't know why I assumed the OP was on Windows :P
– fmark
Nov 25 '10 at 8:30


os.stat


win32file.DeviceIoControl





"On some Unix systems (such as Linux), the following attributes may also be available: st_blocks (number of blocks allocated for file), st_blksize (filesystem blocksize)..." – i.e. that's not portable, and you should at least catch the exception that is raised when these members aren't available.
– Philipp
Nov 25 '10 at 9:15





Careful, this is wrong! On Linux, st.st_blocks is always in units of 512 bytes, while st.st_blksize is a filesystem blocksize (typically 4096 bytes). The real usage is st.st_blocks * 512. See linux.die.net/man/2/stat for details.
– Jim Paris
Aug 5 '13 at 16:24


st.st_blocks


st.st_blksize


st.st_blocks * 512





No, you're both wrong: st.st_blocks is NOT ALWAYS in units of 512 bytes. On my machine it is in units of 1024 (which is strange indeed). Additionally, the answer is wrong because st_blksize does not return 1024, it returns the FILE I/O block size, e.g., st_blksize returns 65536 on my machine. For example, on my dell laptop running python 2.7.8 on cygwin on Windows 7, I created a 3000Byte files ("dd if=/dev/zero bs=3000 count=1 of=./testfile.txt") and: os.stat("testfile.txt").st_blocks=4; os.stat("./testfile.txt").st_blksize=65536; the logical size is 3000, on disk is 4096. I will answer below
– hft
Jan 17 '15 at 23:06





Can you please update your answer to refer to @hft's answer below?
– Miserable Variable
Apr 25 at 18:37




UNIX only:


import os
from collections import namedtuple

_ntuple_diskusage = namedtuple('usage', 'total used free')

def disk_usage(path):
"""Return disk usage statistics about the given path.

Returned valus is a named tuple with attributes 'total', 'used' and
'free', which are the amount of total, used and free space, in bytes.
"""
st = os.statvfs(path)
free = st.f_bavail * st.f_frsize
total = st.f_blocks * st.f_frsize
used = (st.f_blocks - st.f_bfree) * st.f_frsize
return _ntuple_diskusage(total, used, free)



Usage:


>>> disk_usage('/')
usage(total=21378641920, used=7650934784, free=12641718272)
>>>



Edit 1 - also for Windows: https://code.activestate.com/recipes/577972-disk-usage/?in=user-4178764



Edit 2 - this is also available in Python 3.3+: https://docs.python.org/3/library/shutil.html#shutil.disk_usage



Use os.stat(filename).st_size to get the logical size of the file. Use os.statvfs(filename).f_bsize to get the filesystem block size. Then use integer division to compute the correct size on disk, as below:


lSize=os.stat(filename).st_size
bSize=os.statvfs(filename).f_bsize
sizeOnDisk=(lSize/bSize+1)*bSize





((lSize-1)/bSize+1)*bSize) might be slightly more accurate. Thanks for correcting my ancient and wrong answer.
– ephemient
Jan 20 '15 at 16:19


((lSize-1)/bSize+1)*bSize)





no problem whatsoever ;)
– hft
Jan 21 '15 at 16:24





Deprecated since version 2.6: The statvfs module has been removed in Python 3. :-( docs.python.org/2/library/statvfs.html
– danodonovan
Aug 12 '15 at 12:36


Deprecated since version 2.6: The statvfs module has been removed in Python 3.





@danodonovan It looks like the statvfs module has been removed in Python 3, but the answer uses the os module. As you can see, the documentation for Python 3 reveals that os.statvfs is still around and has even been updated to include new functionality as recently as Python 3.6.
– bytesized
Jan 10 '17 at 22:11


statvfs


os


os.statvfs



I'm not certain if this is size on disk, or the logical size:


import os
filename = "/home/tzhx/stuff.wev"
size = os.path.getsize(filename)



If it's not the droid your looking for, you can round it up by dividing by cluster size (as float), then using ceil, then multiplying.





That's not the size on disk.
– Ruel
Nov 25 '10 at 8:21





when I used getsize() in windows7,python 2.2, I did get the actual space file occupies. In my case, I crave for the just "file size" not "file space".I wonder how can you get just the file size
– Allan Ruin
Aug 2 '12 at 17:43



To get the disk usage for a given file/folder, you can do the following:


import os

def disk_usage(path):
"""Return cumulative number of bytes for a given path."""
# get total usage of current path
total = os.path.getsize(path)
# if path is dir, collect children
if os.path.isdir(path):
for file_name in os.listdir(path):
child = os.path.join(path, file_name)
# recursively get byte use for children
total += disk_usage(child)
return total



The function recursively collects byte usage for files nested within a given path, and returns the cumulative use for the entire path.
You could also add a print "{path}: {bytes}".format(path, total) in there if you want the information for each file to print.


print "{path}: {bytes}".format(path, total)





After running multiple tests, on Windows 7 this returns the real size, not the size on disk.
– Steven Byrne
Nov 13 '17 at 17:50






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV