[Pythonmac-SIG] file system encoding and type

Henning Hraban Ramm hraban at fiee.net
Mon Jul 28 14:43:01 CEST 2008


How can I detect for some entry in "/Volumes" what kind of volume it  
is, e.g. Harddisk, USB stick, CD/DVD, and what encoding it uses?
For a CD/DVD cataloguing tool I need to detect removable disks.
At the moment I call "mount" like this:

import re, subprocess

def findCD():
     """
     Look for mounted volumes and return the first CD.
     Works probably only on MacOS X, depends on the output of 'mount'
     """
     mountlines = subprocess.Popen('mount',  
stdout=subprocess.PIPE).communicate()[0].split('\n')
     reMountLine = re.compile('(.+) on (/[^(]*)(\((.+)\))?', re.I)
     for ml in mountlines:
         m = reMountLine.match(ml)
         if not m:
             continue
         g = m.groups()
         modes = g[3]
         if modes:
             modes = modes.split(', ')
             if ('nodev' in modes) and ('read-only' in modes):
                 return unicode(g[1].strip(), 'utf-8') # here we need  
to know the FS encoding!
     return None



This is a sample output of "mount" on my Leopard machine:

hraban$ mount
/dev/disk0s2 on / (hfs, local, journaled)
devfs on /dev (devfs, local)
fdesc on /dev (fdesc, union)
map -hosts on /net (autofs, automounted)
map auto_home on /home (autofs, automounted)
/dev/disk1s1 on /Volumes/RTFM (msdos, local, nodev, nosuid, noowners)
/dev/disk2s0 on /Volumes/family 2008 (cd9660, local, nodev, nosuid,  
read-only, noowners)

"RTFM" is my USB stick.
"family 2008" is a CD.

I.e.
- "hfs", "msdos" or "cd9660" give a hint about the file system's  
encoding:
   - hfs: decomposed UTF-8
   - msdos: latin-1
   - cd9660: ?
- "nodev" seems to mark a removable volume
- "read-only" is normally a CD/DVD

I couldn't find anything in Python's standard libraries to get this  
information. Is there?
I would prefer a platform independent solution.



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





More information about the Pythonmac-SIG mailing list