Is there a pythonic way and without shell commands (i.e. with subprocess module) to check if a directory is a mount point?
是否有pythonic方式并且没有shell命令(即使用子进程模块)来检查目录是否是挂载点?
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
1 个解决方案
#1
7
There is an os.path.ismount(path)
.
有一个os.path.ismount(路径)。
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.
如果路径名路径是装入点,则返回True:文件系统中已装入其他文件系统的点。该函数检查路径的父路径,路径/ ..是否在与路径不同的设备上,或路径/ ..和路径是否指向同一设备上的同一个i节点 - 这应该检测所有Unix的挂载点和POSIX变种。
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.
您也可以参考实现(如果您使用的是POSIX系统)。检查macpath.py或ntpath.py以获取其他平台。
#1
7
There is an os.path.ismount(path)
.
有一个os.path.ismount(路径)。
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.
如果路径名路径是装入点,则返回True:文件系统中已装入其他文件系统的点。该函数检查路径的父路径,路径/ ..是否在与路径不同的设备上,或路径/ ..和路径是否指向同一设备上的同一个i节点 - 这应该检测所有Unix的挂载点和POSIX变种。
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.
您也可以参考实现(如果您使用的是POSIX系统)。检查macpath.py或ntpath.py以获取其他平台。