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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With