windows 如何在python中获取驱动器的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8319264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the name of a drive in python
提问by Eric
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:\
:
我有一个有效驱动器号的列表,我想向最终用户提供一个选择。我想向他们展示驱动器的名称。这是一些应该向我显示驱动器名称的代码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
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)?
但是,这输出\\?\Volume{a8b6b3df-1a63-11e1-9f6f-0007e9ebdfbf}\
. 如何获取 Windows 在资源管理器中显示的字符串(例如KINGSTON
,对于我拥有的某个闪存驱动器)?
EDIT:
编辑:
Still not working:
还是行不通:
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)
)
This gives me this error:
这给了我这个错误:
WindowsError: exception: access violation reading 0x3A353FA0
采纳答案by Greg Hewgill
Try the GetVolumeInformation
function instead. It returns the volume label directly.
试试这个GetVolumeInformation
功能吧。它直接返回卷标。
回答by Felix Heide
Why don't you use win32api.GetVolumeInformation?
为什么不使用 win32api.GetVolumeInformation?
import win32api
win32api.GetVolumeInformation("C:\")
outputs
输出
('WINDOWS', 1992293715, 255, 65470719, 'NTFS')
回答by Nicholas Orlowski
Using the above fragment, I filled in the missing(optional, null) arguments as a quick helper:
使用上面的片段,我填写了缺少的(可选的,空的)参数作为快速助手:
import ctypes
kernel32 = ctypes.windll.kernel32
volumeNameBuffer = ctypes.create_unicode_buffer(1024)
fileSystemNameBuffer = ctypes.create_unicode_buffer(1024)
serial_number = None
max_component_length = None
file_system_flags = None
rc = kernel32.GetVolumeInformationW(
ctypes.c_wchar_p("F:\"),
volumeNameBuffer,
ctypes.sizeof(volumeNameBuffer),
serial_number,
max_component_length,
file_system_flags,
fileSystemNameBuffer,
ctypes.sizeof(fileSystemNameBuffer)
)
print volumeNameBuffer.value
print fileSystemNameBuffer.value
This should be copy-and-paste-able.
这应该是可复制和粘贴的。
回答by berdzi
You can execute windows shell cmd and parse the output.
您可以执行 windows shell cmd 并解析输出。
in Python 3.x:
在 Python 3.x 中:
import subprocess
def getDriveName(driveletter):
return subprocess.check_output(["cmd","/c vol "+driveletter]).decode().split("\r\n")[0].split(" ").pop()
print (getDriveName("d:"))
in Python 2.7:
在 Python 2.7 中:
import subprocess
def getDriveName(driveletter):
return subprocess.check_output(["cmd","/c vol "+driveletter]).split("\r\n")[0].split(" ").pop()
print getDriveName("d:")
回答by Louie
- returns driveLetter for the given driveLabel
- returns "notfound" if driveLabel was not found
- 返回给定 driveLabel 的 driveLetter
- 如果未找到 driveLabel,则返回“notfound”
def findDriveByDriveLabel(driveLabel):
def findDriveByDriveLabel(driveLabel):
drvArr = ['c:', 'd:', 'e:', 'f:', 'g:', 'h:', 'i:', 'j:', 'k:', 'l:']
for dl in drvArr:
try:
if (os.path.isdir(dl) != 0):
val = subprocess.check_output(["cmd", "/c vol " + dl])
if (driveLabel in str(val)):
return dl + "/"
except:
print("Error: findDriveByDriveLabel(): exception")
return "notfound"