Find size and free space of the filesystem containing a given file
If you just need the free space on a device, see the answer using os.statvfs()
below.
If you also need the device name and mount point associated with the file, you should call an external program to get this information. df
will provide all the information you need -- when called as df filename
it prints a line about the partition that contains the file.
To give an example:
import subprocess
df = subprocess.Popen(["df", "filename"], stdout=subprocess.PIPE)
output = df.communicate()[0]
device, size, used, available, percent, mountpoint = \
output.split("\n")[1].split()
Note that this is rather brittle, since it depends on the exact format of the df
output, but I'm not aware of a more robust solution. (There are a few solutions relying on the /proc
filesystem below that are even less portable than this one.)
Get hard disk size in Python
For Python 2 till Python 3.3
Notice: As a few people mentioned in the comment section, this solution will work for Python 3.3 and above. For Python 2.7 it is best to use the psutil
library, which has a disk_usage
function, containing information about total, used and free disk space:
import psutil
hdd = psutil.disk_usage('/')
print ("Total: %d GiB" % hdd.total / (2**30))
print ("Used: %d GiB" % hdd.used / (2**30))
print ("Free: %d GiB" % hdd.free / (2**30))
Python 3.3 and above:
For Python 3.3 and above, you can use the shutil
module, which has a disk_usage
function, returning a named tuple with the amounts of total, used and free space in your hard drive.
You can call the function as below and get all information about your disk's space:
import shutil
total, used, free = shutil.disk_usage("/")
print("Total: %d GiB" % (total // (2**30)))
print("Used: %d GiB" % (used // (2**30)))
print("Free: %d GiB" % (free // (2**30)))
Output:
Total: 931 GiB
Used: 29 GiB
Free: 902 GiB
Python script to verify disk space output from Linux
you can try this:
>>> import subprocess
>>> threshold = 10
>>> child = subprocess.Popen(['df', '-h'], stdout=subprocess.PIPE)
>>> output = child.communicate()[0].strip().split("\n")
>>> for x in output[1:]:
... if int(x.split()[-2][:-1]) >= threshold:
... print x
This will List all filesystem that disk usage is 10% or more than 10%
How to find out the free disk space for a given path on a linux shell?
Try df -h
Better yet: df -h .
or df -h path-to-file
Bytes returned by disk_usage from the shutil library doesn't match the directory file size
shutil.disk_usage()
returns statistics for the entire disk (filesystem, volume), not just for the specific directory you pass in.
To compute disk space used by one directory and its subdirectories, see: Calculating a directory's size using Python?
For a good introduction to some of the potential pitfalls, see: https://blogs.msdn.microsoft.com/oldnewthing/20041228-00/?p=36863
Get actual disk space of a file
st = os.stat(…)
du = st.st_blocks * st.st_blksize
Related Topics
Os.Walk Without Hidden Folders
How to Clone a List So That It Doesn't Change Unexpectedly After Assignment
Are Dictionaries Ordered in Python 3.6+
Evaluating a Mathematical Expression in a String
Rolling or Sliding Window Iterator
How to Print Instances of a Class Using Print()
How to Match a Whole Word With a Regular Expression
Python + Selenium: Wait Until Element Is Fully Loaded
How to Set Your Pythonpath in an Already-Created Virtualenv
Simulating Key Press Event Using Python For Linux
How to Read a File Line-By-Line into a List
Why Do I Get Attributeerror: 'Nonetype' Object Has No Attribute 'Something'
How Does Python'S Super() Work With Multiple Inheritance