Python: Get Mount Point on Windows or Linux

后端 未结 3 1043
说谎
说谎 2021-01-05 23:28

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          


        
3条回答
  •  生来不讨喜
    2021-01-06 00:08

    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).

    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.

提交回复
热议问题