Results 1 to 2 of 2

Thread: Finding a CDROM device (Python, if it matters)

  1. #1
    Join Date
    May 2010
    Location
    San Jose, California, USA
    Beans
    16
    Distro
    Ubuntu 10.04 Lucid Lynx

    Finding a CDROM device (Python, if it matters)

    I'm a casual programmer (i.e., I'm no longer a professional, I just program for fun or for my own projects), usually in Python (although I don't think this is a Python-specific question).

    I'm writing a program that catalogs CDROMs, and need to go through the directory of the CDROM given the device.

    On Windows, I use something like

    Code:
    startpoint="D:/"
    for (root, dirs, files) in os.walk(startpoint):
      (stuff)
    Is there an equivalent starting point in Linux?

    I've tried
    Code:
    startpoint="/dev/sr0"
    and
    startpoint="/dev/sr0/"
    but no luck.

    What does work is
    Code:
    startpoint="/media/VOLUMENAME"
    where VOLUMENAME is the name of the mounted CDROM volume; but I need to be able to do this without knowing the volume name.

    Any ideas?

  2. #2
    Join Date
    May 2010
    Location
    San Jose, California, USA
    Beans
    16
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Finding a CDROM device (Python, if it matters)

    After some experimentation, it seems like a working approach is to call the Linux "volname" command (hat-tip to Steven D'Aprano on the python-tutors list for identifying the command for me) to find out the volume, and then use the volume name to specify the directory in /media. Here's what I'm doing now:

    Code:
    import subprocess, os
    def getvolid(mountpoint):
         p = subprocess.Popen(["volname", mountpoint],
             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         (response, errmsg) = p.communicate()
         volid=response.rstrip()
         errmsg = errmsg.rstrip()
         if len(response)==0:
             volid=None
         return (volid, errmsg)
    I use it thus:

    Code:
    cdrom_dev="/dev/sr0"
    (volid, errmsg) = getvolid(cdrom_dev)
    if volid is not None:
        for (r, d, f) in os.walk("/media/"+volid):
            print (r, d, f)
    else:
        print "Error retrieving volume id, '%s'" %(errmsg)
    Last edited by TJRC; November 7th, 2010 at 05:27 PM. Reason: forgot hat-tip

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •