或多或少在它上面说的:在Python中有一种(简单的)方法列出Windows系统中当前正在使用的所有驱动器号吗?
(我的google-fu似乎让我失望了.)
有关:
Ric*_*dle 61
不使用任何外部库,如果这对您很重要:
import string
from ctypes import windll
def get_drives():
drives = []
bitmask = windll.kernel32.GetLogicalDrives()
for letter in string.uppercase:
if bitmask & 1:
drives.append(letter)
bitmask >>= 1
return drives
if __name__ == '__main__':
print get_drives() # On my PC, this prints ['A', 'C', 'D', 'F', 'H']
Run Code Online (Sandbox Code Playgroud)
Aym*_*ieh 58
import win32api
drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1]
print drives
Run Code Online (Sandbox Code Playgroud)
改编自:http: //www.faqts.com/knowledge_base/view.phtml/aid/4670
Sin*_*ion 10
那些看起来更好的答案.这是我的hackish cruft
import os, re
re.findall(r"[A-Z]+:.*$",os.popen("mountvol /").read(),re.MULTILINE)
Run Code Online (Sandbox Code Playgroud)
在RichieHindle的回答中略微说道 ; 它并不是真的更好,但你可以让窗户做出实际的字母表字母
>>> import ctypes
>>> buff_size = ctypes.windll.kernel32.GetLogicalDriveStringsW(0,None)
>>> buff = ctypes.create_string_buffer(buff_size*2)
>>> ctypes.windll.kernel32.GetLogicalDriveStringsW(buff_size,buff)
8
>>> filter(None, buff.raw.decode('utf-16-le').split(u'\0'))
[u'C:\\', u'D:\\']
Run Code Online (Sandbox Code Playgroud)
我写了这段代码:
import os
drives = [ chr(x) + ":" for x in range(65,91) if os.path.exists(chr(x) + ":") ]
Run Code Online (Sandbox Code Playgroud)
它基于@Barmaley 的回答,但具有不使用该string
模块的优点,以防您不想使用它。与@SingleNegationElimination 的回答不同,它也适用于我的系统。
在Google上找到此解决方案,稍微修改一下.看似漂亮的pythonic,不需要任何"异国情调"的进口
import os, string
available_drives = ['%s:' % d for d in string.ascii_uppercase if os.path.exists('%s:' % d)]
Run Code Online (Sandbox Code Playgroud)
如果您只想列出磁盘上的驱动器而不是映射的网络驱动器,这是另一个很好的解决方案。如果您想按不同的属性过滤,只需打印 drps。
import psutil
drps = psutil.disk_partitions()
drives = [dp.device for dp in drps if dp.fstype == 'NTFS']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37256 次 |
| 最近记录: |