1

So, os.statvfs() is deprecated since 2.6, shutil.disk_usage() is not there yet (available in 3). What's left?
EDIT: I don't want to add a new lib at this point so psutil is also out.

I am going to run df in a subprocess and parse the output, is there a better way?

davka
  • 13,974
  • 11
  • 61
  • 86
  • Do you care for compatibility of your code between 2.7 & 3? – GPhilo Dec 17 '18 at 10:35
  • 2
    `os.statvfs()` is deprecated but still works in python 2.7 so why not use it? – ddor254 Dec 17 '18 at 10:37
  • @GPhilo generally yes, at this particular point less – davka Dec 17 '18 at 15:35
  • 1
    If you don't care, just use the deprecated function. It has been replaced (a long time ago) in 3+ but since you're using 2.7 and you don't want to rely on libraries, that's your best shot. – GPhilo Dec 17 '18 at 15:36

3 Answers3

6

I tested it using Anaconda2-5.3.1-Windows-x86_64, psutil came installed:

import psutil

obj_Disk = psutil.disk_usage('/')

print (obj_Disk.total / (1024.0 ** 3),"GB")
print (obj_Disk.used / (1024.0 ** 3),"GB")
print (obj_Disk.free / (1024.0 ** 3),"GB")
print (obj_Disk.percent)

Reference:

https://pypi.python.org/pypi/psutil

Get hard disk size in Python

Sheece Gardazi
  • 480
  • 6
  • 14
2
  1. os.system('df -k /')
  2. psutil.disk_usage('/')
  • 1
    Welcome to Stack Overflow. Thank you for your answer. While these commands may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.[How to Answer](https://stackoverflow.com/help/how-to-answer). Regards. – Elletlar Dec 17 '18 at 11:03
  • Can you add some explanation what are the flags e.g. df, -k representing? I don't know how to use the os command to get free space. – Sheece Gardazi Dec 17 '18 at 11:05
  • thanks. I forgot to mention that I don't want to add a new library. What I'm currently doing is more or less your #1 – davka Dec 17 '18 at 15:33
2

df is primarily intended for human consumption and only sometimes for scripting in shell script. The output of user space commands can sometimes be hard to parse as they're primarily intended for human consumption, though you can pass some arguments to some user space commands to have machine-parseable output. When using languages like Python, os supports most of the commonly used system features, but you can also use high level wrappers like psutil library. I highly recommend psutil if you're doing this often.

If you don't want to bring in third party libraries, I'd recommend using the /sys/class/block special filesystem (or /sys/block if you want to support legacy systems as well), alternatively you can parse /proc/partitions. The /sys, /dev, and /proc special filesystems are stable kernel interfaces designed for use in scripting, you interact with these special files by reading/writing to these special files, most of the interfaces the there are fairly easy to parse as they're designed to be used in shell scripts.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144