Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a directory is a mount point with python 2.7

Is there a pythonic way and without shell commands (i.e. with subprocess module) to check if a directory is a mount point?

Up to now I use:

import os
import subprocess

def is_mount_point(dir_path):
    try:
        check_output([
            'mountpoint',
            path.realpath(dir_name)
        ])
        return True
    except CalledProcessError:
        return False
like image 575
Xxxo Avatar asked Aug 24 '16 13:08

Xxxo


2 Answers

There is an os.path.ismount(path).

Return True if pathname path is a mount point: a point in a file system where a different file system has been mounted. The function checks whether path‘s parent, path/.., is on a different device than path, or whether path/.. and path point to the same i-node on the same device — this should detect mount points for all Unix and POSIX variants.

import os
os.path.ismount(dir_name)  # returns boolean

You may also refer to implementation (if you're on POSIX system). Check macpath.py or ntpath.py for other platforms.

like image 61
Łukasz Rogalski Avatar answered Sep 30 '22 06:09

Łukasz Rogalski


in Python 3.7, use Path.is_mount()

>>> from pathlib import Path
>>> p = Path('/some/mounted/dir/')
>>> p.is_mount()
True
like image 36
SuperNova Avatar answered Sep 30 '22 06:09

SuperNova