Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中获取驱动器的名称_Python_Windows_Ctypes - Fatal编程技术网

如何在python中获取驱动器的名称

如何在python中获取驱动器的名称,python,windows,ctypes,Python,Windows,Ctypes,我有一个有效驱动器号列表,我想向最终用户提供一个选择。我想给他们看驱动器的名称。下面的一些代码应该会显示驱动器的名称F:\: import ctypes kernel32 = ctypes.windll.kernel32 buf = ctypes.create_unicode_buffer(1024) kernel32.GetVolumeNameForVolumeMountPointW( ctypes.c_wchar_p("F:\\"), buf,

我有一个有效驱动器号列表,我想向最终用户提供一个选择。我想给他们看驱动器的名称。下面的一些代码应该会显示驱动器的名称
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


编辑: 仍然不起作用:

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

试试这个函数。它直接返回卷标。

使用上面的片段,我填写了缺少的(可选,null)参数作为快速助手:

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

这应该是可以复制和粘贴的。

为什么不使用win32api.GetVolumeInformation

import win32api
win32api.GetVolumeInformation("C:\\")
输出

('WINDOWS', 1992293715, 255, 65470719, 'NTFS')

您可以执行windows shell cmd并解析输出

在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:"))
在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:")
  • 返回给定driveLabel的driveLetter
  • 如果未找到driveLabel,则返回“notfound”
def FindAddressByDriveLabel(driveLabel):


如果您觉得有用,可以使用下面的代码获取驱动器名

import win32api
import win32con
import win32file

def get_removable_drives():
    drives = [i for i in win32api.GetLogicalDriveStrings().split('\x00') if i]
    #print(drives)
    rdrives = [d for d in drives if win32file.GetDriveType(d) == win32con.DRIVE_REMOVABLE]
    return rdrives

drive_list = get_removable_drives()

for i in drive_list:
    print(win32api.GetVolumeInformation(i)[0]+'('+i+')')


还有三个其他参数尚未传递:
lpVolumeSerialNumber
lpMaximumComponentLength
lpFileSystemFlags
。文档中的“可选”名称并不意味着您可以简单地将其省略,但如果您对该值不感兴趣,可以将
NULL
作为指向该信息的指针传递。@GregHewgill:Awesome。谢谢
import win32api
import win32con
import win32file

def get_removable_drives():
    drives = [i for i in win32api.GetLogicalDriveStrings().split('\x00') if i]
    #print(drives)
    rdrives = [d for d in drives if win32file.GetDriveType(d) == win32con.DRIVE_REMOVABLE]
    return rdrives

drive_list = get_removable_drives()

for i in drive_list:
    print(win32api.GetVolumeInformation(i)[0]+'('+i+')')