By Vasudev Ram
While using Python's os.path module in a project, I got the idea of using it to do a quick-and-dirty check for what drives exist on a Windows system. Actually, not really the physical drives, but the drive letters, that may in reality be mapped any of the following: physical hard disk drives or logical partitions of them, CD or DVD drives, USB drives, or network-mapped drives.
The script, drives.py (below), has two functions, drives() and drives2(). The drives() function prints the required information. The drives2() function is more modular and hence more reusable, since it returns a list of detected drive letters; in fact, drives() can be implemented in terms of drives2().
As we can see, the program relies on the fact that a drive specification like "C:" is considered as a path on Windows, since it means "the current directory on drive C". That is why os.path.exists() works for this use. The program does not use a real OS API that returns information about available drives.
I have only tested it a few times, on my machine. It could be that in other tests, or on other machines, it may fail for some reason, such as a drive that is present but inaccessible for some reason (permissions, hardware issue or other). If you try it and get any errors, I'd be interested to know the details - please leave a comment. Thanks.
- Vasudev Ram - Online Python training and consultingGet updates on my software products / ebooks / courses. Jump to posts: Python DLang xtopdfSubscribe to my blog by emailMy ActiveState recipes
While using Python's os.path module in a project, I got the idea of using it to do a quick-and-dirty check for what drives exist on a Windows system. Actually, not really the physical drives, but the drive letters, that may in reality be mapped any of the following: physical hard disk drives or logical partitions of them, CD or DVD drives, USB drives, or network-mapped drives.
The script, drives.py (below), has two functions, drives() and drives2(). The drives() function prints the required information. The drives2() function is more modular and hence more reusable, since it returns a list of detected drive letters; in fact, drives() can be implemented in terms of drives2().
from __future__ import print_functionI ran it thusly:
'''
Author: Vasudev Ram
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product store: http://gumroad.com/vasudevram
'''
from os.path import exists
def drives():
# Limit of 'N' chosen arbitrarily.
# For letters in the first half of the alphabet:
for drive in range(ord('A'), ord('N')):
print('Drive', chr(drive), 'exists:', exists(chr(drive) + ':'))
print()
drives()
print()
def drives2():
drive_list = []
for drive in range(ord('A'), ord('N')):
if exists(chr(drive) + ':'):
drive_list.append(chr(drive))
return drive_list
print("The following drives exist:", drives2())
$ python drives.pyAnd the output was thisly:
Drive A exists: False
Drive B exists: False
Drive C exists: True
Drive D exists: True
Drive E exists: True
Drive F exists: False
Drive G exists: False
Drive H exists: True
Drive I exists: False
Drive J exists: False
Drive K exists: False
Drive L exists: False
Drive M exists: False
The following drives exist: ['C', 'D', 'E', 'H']
As we can see, the program relies on the fact that a drive specification like "C:" is considered as a path on Windows, since it means "the current directory on drive C". That is why os.path.exists() works for this use. The program does not use a real OS API that returns information about available drives.
I have only tested it a few times, on my machine. It could be that in other tests, or on other machines, it may fail for some reason, such as a drive that is present but inaccessible for some reason (permissions, hardware issue or other). If you try it and get any errors, I'd be interested to know the details - please leave a comment. Thanks.
- Vasudev Ram - Online Python training and consultingGet updates on my software products / ebooks / courses. Jump to posts: Python DLang xtopdfSubscribe to my blog by emailMy ActiveState recipes