简体   繁体   中英

Check if a directory is a mount point with python 2.7

Is there a pythonic way and without shell commands (ie 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

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.

in Python 3.7, use Path.is_mount()

>>> from pathlib import Path
>>> p = Path('/some/mounted/dir/')
>>> p.is_mount()
True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM