You don't have a mac to test this on, but I've come up with a slightly less hack-y way to get disk size on a mac. It will only work on a mac since it relies on diskutil, but thats easy enough to work around.
This is what I have. I can clean it up a little, I just wanted to make sure it worked.
And this is the output for my 4gb usb drive:
and an 80gb hdd:
When I attach this to a gui (which I've worked on a little) I'll have the disk ID and size returned together to make a dropdown menu the use can pick from.
For the cli part I'd probably have it return a tuple like ('disk0', 79998918144) for every disk in the system, but its easy enough to tweak.
According to the docs, plistutil is in every python installation, regardless of platform, but you shouldtry it out. I can't be arsed to reboot into windows to see for myself.
This is what I have. I can clean it up a little, I just wanted to make sure it worked.
Code:
import sys, subprocess
import plistlib
if sys.platform == 'darwin':
list_command = "diskutil list -plist"
try:
output = subprocess.check_output(list_command, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
sys.exit("I don't even know. Just. I don't know. %s" % err)
list_plist = plistlib.readPlistFromString(output)
disk_list = list_plist["WholeDisks"]
print disk_list
chosen_disk = raw_input("Choose a disk: ")
size_command = "diskutil info -plist %s" % chosen_disk
try:
output = subprocess.check_output(size_command, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
sys.exit("OH NO! The chosen disk could not be found. %s" % err )
size_plist = plistlib.readPlistFromString(output)
disk_size = size_plist["TotalSize"]
print "%s's size is %s" % (chosen_disk, disk_size)
else:
print "not a mac, doofus"
#do whatever works for other platforms here
And this is the output for my 4gb usb drive:
Code:
$ python /Users/Me/Desktop/disklist.py
['disk0', 'disk1', 'disk2']
Choose a disk: disk2
disk2's size is 4004511744
and an 80gb hdd:
Code:
$ python /Users/Me/Desktop/disklist.py
['disk0', 'disk1', 'disk2']
Choose a disk: disk0
disk0's size is 79998918144
When I attach this to a gui (which I've worked on a little) I'll have the disk ID and size returned together to make a dropdown menu the use can pick from.
For the cli part I'd probably have it return a tuple like ('disk0', 79998918144) for every disk in the system, but its easy enough to tweak.
According to the docs, plistutil is in every python installation, regardless of platform, but you shouldtry it out. I can't be arsed to reboot into windows to see for myself.