[Pythonmac-SIG] detecting mounted disks

Henning Hraban Ramm hraban at fiee.net
Sun Jul 16 19:46:18 CEST 2006


Am 2006-07-16 um 19:06 schrieb Bob Ippolito:
> On Jul 16, 2006, at 4:28 AM, Henning Hraban Ramm wrote:
>
>> How can I detect if an entry in MacOSX's /Volumes (or more generally:
>> any mount point in a UNIX-like OS) is...
>> - a local harddisk
>> - a network volume
>> - a CD/DVD-ROM
>> - something other (USB stick...)
>> ?
>>
>> I guess I could get some info from 'mount', but isn't there already a
>> Python function?
>> o.path's ismount and isdir aren't enough.
>
> You'd probably have to get that information from IOKit, which isn't  
> wrapped by Python at all.

For now I'm content with 'mount':

import subprocess, tempfile, re, pprint

tf = tempfile.TemporaryFile()
subprocess.Popen('/sbin/mount', stdout=tf)
tf.seek(0)
mountlines = tf.readlines()
tf.close()

reMountLine = re.compile('(.+) on (/[^\s]*)( \((.+)\))?', re.I)

mounts = []

for ml in mountlines:
     m = reMountLine.match(ml)
     if not m: next
     g = m.groups()
     type = 'unknown'
     modes = g[3]
     if modes:
         modes = modes.split(', ')
         if ((g[0].startswith('/dev/')) and ('local' in modes)):
             type = 'hd'
         elif g[1] == '/dev':
             type = 'dev'
         elif g[0].startswith('automount'):
             type = 'automount'
         if 'nodev' in modes:
             type = 'changeable'
             if 'read-only' in modes:
                 type = 'cd'
     mounts.append([g[1], g[0], modes, type])

pp = pprint.PrettyPrinter()
pp.pprint(mounts)


Greetlings from Lake Constance!
Hraban
---
http://www.fiee.net
http://www.cacert.org (I'm an assurer)




More information about the Pythonmac-SIG mailing list