Python:在Windows或Linux上获取挂载点 [英] Python: Get Mount Point on Windows or Linux

查看:270
本文介绍了Python:在Windows或Linux上获取挂载点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数来确定目录是否是驱动器的安装点. 我已经找到了适合Linux的代码:

I need a function to determine if a directory is a mount point for a drive. I found this code already which works well for linux:

def getmount(path):
  path = os.path.abspath(path)
  while path != os.path.sep:
    if os.path.ismount(path):
      return path
    path = os.path.abspath(os.path.join(path, os.pardir))
  return path

但是我不确定如何在Windows上使用它.我可以仅假设挂载点是驱动器号(例如C :)吗?我相信可以在Windows上进行网络安装,因此我也希望能够检测到该安装.

But I'm not sure how I would get this to work on windows. Can I just assume the mount point is the drive letter (e.g. C:)? I believe it is possible to have a network mount on windows so I'd like to be able to detect that mount as well.

推荐答案

Windows并没有使用它们来称它们为装载点" [ edit :现在使用了,请参见下文!",并且您可以找到的两种典型/传统语法是驱动器号,例如Z:,否则为\\hostname(带有两个前导反斜杠:请谨慎转义,或在Python fpr中使用r'...'表示法作为文字字符串).

Windows didn't use to call them "mount points" [edit: it now does, see below!], and the two typical/traditional syntaxes you can find for them are either a drive letter, e.g. Z:, or else \\hostname (with two leading backslashes: escape carefully or use r'...' notation in Python fpr such literal strings).

编辑:由于支持NTFS 5.0挂载点,但根据subprocess.Popen执行它,并检查其输出.

edit: since NTFS 5.0 mount points are supported, but according to this post the API for them is in quite a state -- "broken and ill-documented", the post's title says. Maybe executing the microsoft-supplied mountvol.exe is the least painful way -- mountvol drive:path /L should emit the mounted volume name for the specified path, or just mountvol such list all such mounts (I have to say "should" because I can't check right now). You can execute it with subprocess.Popen and check its output.

这篇关于Python:在Windows或Linux上获取挂载点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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