[Tutor] Reading the CDROM in Linux

Carlos Daniel Ruvalcaba Valenzuela clsdaniel at gmail.com
Sat Nov 6 05:03:26 CET 2010


This is more of a Linux question, but here is some advice:

All files under /dev are more or less raw representations of the
devices, meaning that /dev/cdrom or /dev/sr0 files represent the CDROM
devices, however this is for raw access to the device data, now really
for normal use, the sistem may "mount" this device in another place
(like inside the /media directory), so that a directory will represent
the contents (filesystem) of the device, which is what you may want to
use.

In the example that you are using, you may call 'df' from python and
parse the data, which more or less is presented in the following
format: device, space, used space, mounted directory. What you need
the the directory where the device is mounted (the last data), and
then you may use walk on that, taking care that the directory is under
your media directory so that you won't walk the root directory
entirely (your hard disk).

You may want to use os.popen or one of the many process modules to
call 'df', then parse each line by spliting the spaces or tabs, then
the rest is more or less what you are doing right now.

Regards,
Carlos Ruvalcaba

On Fri, Nov 5, 2010 at 5:59 PM, Terry Carroll <carroll at tjc.com> wrote:
> Alan Gauld wrote:
>
>> I don't use Ubuntu so don;t know the standard anmswer
>> there but it will depend on where the CD is mounterd.
>>
>> I usually mount cdroms on /dev/cdrom
>
> That's what I figured; I now realize I didn't say so in my email, but it's
> mounted at /dev/sr0, which is where I came up with that mount point.
>
>
>  tjrc at vraspberry:~$ df
>  . . .
>
>  /dev/sr0               614350    614350         0 100% /media/MP_04_074
>
>
> MP_04_074 is the volume name.
>
> But in python:
>
>>>> import os
>>>> for (r, d, f) in os.walk("/dev/sr0"): print r, d, f
>
> ...
>>>>
>>>> for (r, d, f) in os.walk("/dev/sr0/"): print r, d, f
>
> ...
>>>>
>>>> for (r, d, f) in os.walk("/dev/cdrom/"): print r, d, f
>
> ...
>>>>
>>>> for (r, d, f) in os.walk("/dev/cdrom"): print r, d, f
>
> ...
>>>>
>>>> for (r, d, f) in os.walk("/dev/cdrom0"): print r, d, f
>
> ...
>>>>
>>>> for (r, d, f) in os.walk("/dev/cdrom0/"): print r, d, f
>
> ...
>
>
> None of those work; but:
>
>
>>>> for (r, d, f) in os.walk("/media/MP_04_074"): print r, d, f
>
> ...
> /media/MP_04_074 ['directory1'] []
> (etc.)
>
>
> What I can't figure out is how to do this if my program does not know the
> volume name.  I won't know the colume name in advance, and in fact, I'll be
> processing one CDROM, replacing it and processing another, etc.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list