[Tutor] Reading the CDROM in Linux

Terry Carroll carroll at tjc.com
Sat Nov 6 04:58:27 CET 2010

On Sat, 6 Nov 2010, Steven D'Aprano wrote:

> Terry Carroll wrote:
>> I have a program that traverses the directory of a CDROM using os.walk. I 
>> do most of my work on Windows, but some on (Ubuntu) Linux, and I'd like 
>> this to work in both environments.
>> On Windows, I do something along the lines of this:
>>   startpoint="D:/"
> What if the user has two CD drives? What if they have a second hard disk 
> mounted on D:/, and a network drive on E:/, and use F:/ or A:/ or Z:/ for the 
> CD drive?

D:/ doesn't enter into it.  That's on Windows, I'm asking about Linux.  I 
used "D:/" to show a single example of what works on Windows to explain 
what I am looking for on Linux.

In practice, the drive specification will be coming from a config file. 
It would be D:? on some systems, E:/ on others or maybe both.

But my question is not about Windows, which I already have covered.  My 
question is, to put it succinctly:

How can one use os.walk to walk a directory structure of a CDROM on LInux 
when the volume name is not known?

> On Unix and Linux systems, there are two conventions for mounting external 
> media. One is that if you, the user, mount something by hand using the 
> "mount" command, it gets placed in /mnt (old-school Unix sys admins had

> keyboards without vowels *wink*). Often people would use subdirectories under 
> /mnt:
> /mnt/cdrom
> /mnt/floppy
> are the two most common ones.

No such luck:

tjrc at vraspberry:~$ ls -pal /mnt
total 8
drwxr-xr-x  2 root root 4096 2010-04-23 03:23 ./
drwxr-xr-x 23 root root 4096 2010-10-04 10:42 ../

tjrc at vraspberry:~$ ls -pal /mnt/cdrom
ls: cannot access /mnt/cdrom: No such file or directory
tjrc at vraspberry:~$ ls -pal /mnt/floppy
ls: cannot access /mnt/floppy: No such file or directory

> The other convention is that modern window/desktop managers like KDE and 
> Gnome will automatically mount devices by name under /media.

Yes, I mentioned this, but it requires knowing the volume name.

> If you only have one CD drive, and no other devices mounted, you can 
> just look at /media and walk over that without caring what the CD drive 
> is called. In other words, just use /media as the starting point, and 
> let os.walk discover the name of the CD under it.

But that has the same problem I already mentioned in the prior note: what 
if there's more than one device?  The same thing you pointed out above 
about D:/

> Well that was easy. You need to query the external tool volname, which
> should be present on just about any Linux system.
> Use the subprocess module to call "volname /dev/cdrom".

Aha, this looks like it will work; I was starting to think along these 
lines; I was thinking of reading the output of df, but this is cleaner.

More information about the Tutor mailing list