Quick-and-dirty drive detector in Python (Windows)

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().
from __future__ import print_function

'''
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())
I ran it thusly:
$ python drives.py
And 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 consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

Subscribe to my blog by email

My ActiveState recipes

::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com



自怼圈

DU21.3
关于 ~ DebugUself with DAMA ;-)


关注公众号, 持续获得相关各种嗯哼:
zoomquiet


粤ICP备18025058号-1
公安备案号: 44049002000656 ...::