12

I am using Python2.6. I am trying to list the disk drives that a system may have.

On Windows, it may be something like C:/, D:/, E:/, etc. On Linux, it may be something like /boot, /media/SDCard, etc. And I don't know what it's like on a Mac. Maybe something under /Volumes.

Does anyone know of a cross platform way (that is, one which works on Linux, Windows and Mac) in Python?

Thanks!

jww
  • 97,681
  • 90
  • 411
  • 885
Thomas O
  • 6,026
  • 12
  • 42
  • 60
  • What is the end result you want to achieve with this? – Pekka Aug 29 '10 at 20:50
  • I am building an interface for users to select disk drives attached to the computer. It will need to know the space available on the drives, and have the ability to format them with FAT32 filesystems (maybe) and be able to access files. – Thomas O Aug 29 '10 at 20:54
  • suggestion: stub out the selection to work with what you have access to, get that to work, and then make the comparatively easy device selection. And do be careful with formating, in case that doesn't go without saying. – msw Aug 29 '10 at 21:16
  • /etc/fstab and the output of mount are obvious answers for the Linux side. – Andrew Apr 07 '12 at 23:08

4 Answers4

18

The psutil package (https://pypi.python.org/pypi/psutil) has a disk_partitions function.

Windows:

>>> import psutil
>>> psutil.disk_partitions()
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='E:\\', mountpoint='E:\\', fstype='', opts='cdrom'), sdiskpart(device='F:\\', mountpoint='F:\\', fstype='NTFS', opts='rw,fixed')]

Linux:

>>> import psutil
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,errors=remount-ro'), sdiskpart(device='/dev/sr0', mountpoint='/media/VBOXADDITIONS_4.3.10_93012', fstype='iso9660', opts='ro,nosuid,nodev,uid=1000,gid=1000,iocharset=utf8,mode=0400,dmode=0500,uhelper=udisks')]
Eric Smith
  • 2,739
  • 2
  • 30
  • 29
  • On OS X, my drive has a name that shows up in Finder (Macintosh HD... I'm pretty sure this is the default name.) Any idea how I could find that name, because this is what shows up... `sdiskpart(device='/dev/disk0s2', mountpoint='/', fstype='hfs', opts='rw,local,rootfs,dovolfs,journaled,multilabel')`. – ArtOfWarfare Apr 15 '15 at 00:13
  • I don't know OS X well, but I'd suspect that would be the label for the disk. Maybe a call out to [disklabel](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/disklabel.8.html)? – Eric Smith Apr 15 '15 at 16:26
3

There isn't really a unified naming scheme for Linux devices that guarantees you a formatable block device. There are conventions, but they can vary widely and I can call my thumb-drive /Thomas/O if I want and there is no cross-platform way in Python to know:

  1. That /Thomas/O corresponds to /dev/sdf1
  2. That /dev/sdf1 can have a FAT32 filesystem made on it
  3. That /dev/sdf is not preferred to /dev/sdf1

I'm pretty sure that neither is there a cross-platform Python module which will allow you to determine that H:/ is formattable on a Windows system but that Z:/ is not.

Each system will require its own specific checks and validations which you could best learn from studying open-source disk manipulation software.

msw
  • 42,753
  • 9
  • 87
  • 112
  • I was thinking of how a program like gparted does it. But, that is not cross platform, only Unix (or even Linux), right? The drives do not necessarily have to be formattable. Just, they should be attached or internal media. As I'm mostly dealing with SD cards, I'm just listing /media under Linux, /Volumes on Mac, and all disk drives (C:/, D:/ etc.) on Windows. – Thomas O Aug 29 '10 at 21:39
  • Don't ponder it, steal it. The ideas, I mean, but the source too, unless you like reinventing wheels. http://sourceforge.net/projects/gparted/files/gparted/ – msw Aug 29 '10 at 22:31
  • And there is no guarantee at all that /media will even exist on Linux or that an unformatted drive will be mounted for you to see. Even NT/Vista/7 allows mounting a device on an arbitrary directory in the Unixy way. – msw Aug 29 '10 at 22:37
2

Eric Smith's answer to use psutil works well for me on Windows, but on OS X, I prefer this:

from os import listdir
listdir('/Volumes')

It gives you back the human readable names that, at least in my case, would be preferable (IE, it gives you Macintosh HD instead of / or /dev/disk0s2.)

Community
  • 1
  • 1
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
2

I don't see a way in psutil to include net mounts on Windows. I.e., \foobar\home is mapped to X:, but X: does not appear in the list returned by psutil.disk_partitions() (local drives are).

Update: To include net drives in the returned list, simply use:

psutil.disk_partitions(all=True)

Works quite well.

ivanlan
  • 979
  • 6
  • 8