Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross platform way to list disk drives on Linux, Windows and Mac using Python?

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!

like image 823
Thomas O Avatar asked Aug 29 '10 20:08

Thomas O


1 Answers

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')]
like image 153
Eric Smith Avatar answered Sep 24 '22 00:09

Eric Smith