How to List Disks in Python

Listing disks in Python varies between operating systems since there is no universal way to access partition information across all systems. However, there are a few libraries that can be used to read files on GNU/Linux and Mac OS X that list partition information and Windows-specific libraries for listing information on a Microsoft Windows machine. Depending on your application, you will need to develop your application to use one or the other to acquire the information you need.

Instructions

  1. Microsoft Windows

    • 1

      Make sure that you utilize the win32 extensions by prefacing your script with "import win32api" along with your other library imports.

    • 2

      Place the drive listing into a variable using "driveslist = win32api.GetLogicalDriveStrings()".

    • 3

      Parse the list for viewing with "driveslist = driveslist.split('\000')[:-1]", then "print drives" to output the list.

    GNU/Linux and Mac OS X

    • 4

      Begin your script with your imports, the external libraries you will be calling throughout your program, then add in the OS tools library with the "import os" line. The OS tools library provides dozens of functions for accessing important system information on UNIX-like systems.

    • 5

      Read the /proc/partitions file into a variable using "p = popen("cat /proc/partitions")". The reason this works is because the /proc/partitions file is accessible by any user, unlike another solution using fdisk or another application.

    • 6

      Pipe the p variable so that you can print the text with the line "drives = p.read()", then use "print drives" to display the list. The read() function draws the raw data and places it in the drives variable so that it can be displayed using the print function.

Related Searches:

References

Comments

Related Ads

Featured