如何在python中获取驱动器的名称 [英] How can I get the name of a drive in python

查看:347
本文介绍了如何在python中获取驱动器的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效的驱动器号列表,我想向最终用户提供一个选择。我想向他们展示驱动器的名称。这是一些代码,应该向我显示驱动器 F的名称:\

I have a list of valid drive letters, and I want to present a choice to the end user. I'd like to show them the names of the drives. Here's some code that should show me the name of drive F:\:

import ctypes

kernel32 = ctypes.windll.kernel32
buf = ctypes.create_unicode_buffer(1024)

kernel32.GetVolumeNameForVolumeMountPointW(
    ctypes.c_wchar_p("F:\\"),
    buf,
    ctypes.sizeof(buf)
)

print buf.value

但是,此输出 \\?\Volume {a8b6b3df-1a63-11e1-9f6f-0007e9ebdfbf} \ 。如何获得Windows在资源管理器中显示的字符串(例如,对于我拥有的某个闪存驱动器,为 KINGSTON )?

However, this outputs \\?\Volume{a8b6b3df-1a63-11e1-9f6f-0007e9ebdfbf}\. How can I get the string that windows shows in explorer (eg, KINGSTON, for a certain flash drive I own)?

仍然无法正常工作:

volumeNameBuffer = ctypes.create_unicode_buffer(1024)
fileSystemNameBuffer = ctypes.create_unicode_buffer(1024)

kernel32.GetVolumeInformationW(
    ctypes.c_wchar_p("C:\\"),
    volumeNameBuffer,
    ctypes.sizeof(volumeNameBuffer),
    fileSystemNameBuffer,
    ctypes.sizeof(fileSystemNameBuffer)
)

这给了我这个错误:

WindowsError: exception: access violation reading 0x3A353FA0


推荐答案

尝试 GetVolumeInformation 函数代替。它直接返回音量标签。

Try the GetVolumeInformation function instead. It returns the volume label directly.

这篇关于如何在python中获取驱动器的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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