[Tutor] CD ROM Drive

Tim Golden mail at timgolden.me.uk
Wed Jul 16 14:02:53 CEST 2008


Neven Goršić wrote:
> Hi!
> 
> I am using Python 2.5.2 on WinXP Pro and I want to detect all disk drives.
> I have C:, D: and E: hard disks and F: DVD ROM.

Use WMI:

http://timgolden.me.uk/python/wmi_cookbook.html#find-drive-types

(mutatis mutandis)


> When I try to use os.access method with writing checking I get True
> testing DVD ROM Drive with DVD media inside (which is not writable).
> 1. Why? DVD disk is not writable!
> 
>>>> import os
>>>> print os.access('F:\\',os.W_OK)
> True

In short because os.access always returns True for
*directories* as opposed to *files* on Windows
because it only checks the read-only flag and the
read-only flag has no real meaning on a directory
on Windows.

> 2. When I remove DVD Disk from the drive I get unswer False.


It returns False when there's no media because that
constitutes an exception from the OS and the function
automatically returns False in that case.

[... snip stuff about "Insert disk..." ...]

Frankly this is a bit tricky. I'm not sure to what extent you can
get in ahead of the shell. A couple of possibilities, assuming
I've understood the situation. You can monitor device insertion
and removal either via the Win32 or shell APIs or via WMI (which, I imagine,
uses that API under the covers). The latest pywin32 modules have
added more support for this kind of thing, which you previously
had to do via ctypes.

If you're interested in pursuing this, I can rustle up some code.

> 3. Are there some other way to get list of all disk drives and DVD ROM drives?
> I hoped that I can do that with modules that comes with standard
> Python instalation.

See above. Although my solution uses external modules[1][2], you *can* do
the same using ctypes, but why bother? Just install the 

TJG

[1] http://pywin32.sf.net
[2] http://timgolden.me.uk/python/wmi.html



More information about the Tutor mailing list