可靠且“尽可能便携"从设备名称映射到Python中的挂载点的方法 [英] Reliable and "as portable as possible" way to map from device name to mountpoint in Python

查看:92
本文介绍了可靠且“尽可能便携"从设备名称映射到Python中的挂载点的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用设备名称(例如:/dev/disk2)并确定在Python中将其挂载的位置(例如:/mnt/cdrom/Volumes/RANDLABEL).

I want to be able to take a device name (eg: /dev/disk2) and determine where (if anywhere) it's mounted (eg: /mnt/cdrom or /Volumes/RANDLABEL) in Python.

我可以执行此操作的一种方法是运行dfmount然后解析输出,但这似乎很俗气且不可靠.例如,mount使用" on "作为设备和安装点之间的分隔符.尽管可能性很小,但其中任何一个都可能包含该字符串,从而使输出不明确.

One way I can do this is to run df or mount and then parse the output, but this seems pretty cheesy and unreliable. For example, mount uses " on " as the delimiter between the device and the mountpoint. While very unlikely, either of these could potentially include that very string, making the output ambiguous.

在Linux上,我可以阅读/proc/mounts,但是例如,这在Mac OS X上不起作用.

On Linux I could read /proc/mounts, but this won't work on Mac OS X, for example.

因此,我正在寻找一种方法来以可靠的方式找到设备的挂载点(即:可以处理任意(合法)设备/挂载点名称)并且尽可能地可移植". (我猜测可能无法移植到Windows –我不确定它是否甚至具有类似的设备挂载点概念.)我特别希望在Linux和OS X上都可以使用的东西.

So I'm looking for a way to find the mountpoint for a device in a way that's reliable (ie: can deal with arbitrary (legal) device/mountpoint names) and is "as portable as possible". (I'm guessing that portability to Windows might not be possible -- I'm not sure if it even has an analogous concept of device mountpoints.) I particularly want something that will work on both Linux and OS X.

推荐答案

(根据我的评论:mtab是标准的Linux方式.在FreeBSD,Mac OS X或Solaris上不存在.前两个具有getfsstat(2)getmntinfo(2)系统调用;在Solaris上,您可以使用getmntent(3C).不幸的是,当前安装的文件系统列表不是由POSIX AFAIK定义的,因此在不同的平台上有很大的不同.)

(From my comment above: mtab is the standard Linux way. It doesn't exist on FreeBSD, Mac OS X or Solaris. The former two have the getfsstat(2) and getmntinfo(2) system calls; on Solaris you can use getmntent(3C). Unfortunately the list of currently-mounted filesystems is not defined by POSIX AFAIK, so it is wildly different on different platforms.)

PyPI的 PSI软件包中有一个实验性的mount模块,该模块似乎已尝试可以将所有特定于平台的方法捆绑为一个简单的抽象,并且在Mac OS X(Darwin),AIX,Linux和Solaris上都可以使用.达尔文模块可能适用于* BSD.

There's the experimental mount module in the PSI package from PyPI, which appears to attempt to bundle all the platform-specific methods into a simple abstraction, and which is advertised as working on Mac OS X (Darwin), AIX, Linux and Solaris. The Darwin module probably works on *BSD.

这篇关于可靠且“尽可能便携"从设备名称映射到Python中的挂载点的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆