我正在使用os.statvfs来查找卷上的可用空间-除了查询特定路径的可用空间之外,我还希望能够遍历所有卷。我目前正在Linux上工作,但理想情况下,我希望它可以在Linux上返回["/", "/boot", "home"]和在Windows上返回["C:\", "D:\"]

最佳答案

对于Linux,如何解析/etc/mtab/proc/mounts?或者:

import commands

mount = commands.getoutput('mount -v')
lines = mount.split('\n')
points = map(lambda line: line.split()[2], lines)

print points

对于Windows,我发现了以下内容:
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()

还有这个:
from win32com.client import Dispatch
fso = Dispatch('scripting.filesystemobject')
for i in fso.Drives :
    print i

试试那些,也许他们有帮助。

这也应该有所帮助:Is there a way to list all the available drive letters in python?

关于python - 如何从Python枚举文件系统?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3327528/

10-14 17:14
查看更多