[Tutor] Test If File System is mounted in Linux

Bill Campbell bill at celestial.net
Fri Mar 17 18:30:34 CET 2006


On Fri, Mar 17, 2006, Michael Lange wrote:
>On Fri, 17 Mar 2006 00:36:35 -0700
>fortezza-pyt <fortezza-pyt at fortezzazone.org> wrote:
>
>> If there a semi-standard way to test if a file system has been mounted 
>> or not using Python? In the Linux command line, I can type "mount" and 
>> see all mounted file system, and then see if the one I am looking for is 
>> in the list. While I could replicate this with
>> Python, I am curious if there is an easier way.
>> 
>
>Hi Fortezza,
>
>try os.path.ismount() .

That's fine if one knows what the mount points are.

The more general problem is to get a list of mounted file systems.

The most standard way I've found to get a list of mounted systems is to
parse the output of the gdf program from the gnu fileutils compiled with
the program prefix `g' so it's called gdf.  The gdf program handles a lot
of the dirty work, and its output is standard regardless of the underlying
system, which you can't say about the system's mount or df commands.

The routine below is one I've used for years (actually I used a perl
version for a long time before switching primarily to python :-).

# This aren't real file systems.
pseudofilesys = \
    dict(map((lambda x: (x, 1)), ('none', 'shmfs', 'procfs', 'tmpfs')))

gdf_cols = ('filesys', 'blocks', 'used', 'avail', 'use', 'dir')

def mounted():
    '''Get Mounted File Systems'''
    df = os.popen('gdf 2>/dev/null', 'r')
    df.readline() # skip first line
    mounted = []
    for line in df.readlines():
        line = line.strip()
        rec = dict(zip(gdf_cols, line.split(None, 5)))
        filesys = rec['filesys']
        dir = rec.get('dir')
        if (
            dir and not (filesys.find(':') >= 0
            or pseudofilesys.get(filesys))
        ): mounted.append(dir)
    df.close()
    return mounted

Bill
--
INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Systems, Inc.
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676

``I presume you all know who I am.  I am humble Abraham Lincoln.  I have been
solicited by many friends to become a candidate for the legistlature.  My
politics are short and sweet, like the old woman's dance.  I am in favor of
a national bank ... in favor of the internal improvements system, and a
high protective tariff.'' -- Abraham Lincoln, 1832


More information about the Tutor mailing list