Python Tips: get the disk’s free size of Linux

项月亮
1 min readJan 22, 2020

Using Python module statvfs

import os
path = "/data"
# specify any of a directory on specific disk.
info = os.statvfs(path)print(info)
# output example: os.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=5153733, f_bfree=454587, f_bavail=188381, f_files=1280000, f_ffree=1197286, f_favail=1197286, f_flag=4096, f_namemax=255)
used_size = (info.f_bsize*info.f_blocks)/(1024*1024*1024)
free_size = (info.f_bsize*info.f_bfree)/(1024*1024*1024)
# GB size
print(free_size)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response