繁体   English   中英

如何从Python枚举文件系统?

[英]How can I enumerate filesystems from Python?

我正在使用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

尝试那些,也许他们会帮助。

这也应该有帮助: 有没有办法列出python中所有可用的驱动器号?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM