简体   繁体   English

如何从Python枚举文件系统?

[英]How can I enumerate filesystems from Python?

I'm using os.statvfs to find out the free space available on a volume -- in addition to querying free space for a particular path, I'd like to be able to iterate over all volumes. 我正在使用os.statvfs来查找卷上的可用空间 - 除了查询特定路径的可用空间外,我还希望能够遍历所有卷。 I'm working on Linux at the moment, but ideally would like something which returns ["/", "/boot", "home"] on Linux and ["C:\\", "D:\\"] on Windows. 我目前正在Linux上工作,但理想情况下想在Linux上返回["/", "/boot", "home"]在Windows上返回["C:\\", "D:\\"]

For Linux how about parsing /etc/mtab or /proc/mounts ? 对于Linux,如何解析/etc/mtab/proc/mounts Or: 要么:

import commands

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

print points

For Windows I found something like this: 对于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()

and this: 还有这个:

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

Try those, maybe they help. 尝试那些,也许他们会帮助。

Also this should help: Is there a way to list all the available drive letters in python? 这也应该有帮助: 有没有办法列出python中所有可用的驱动器号?

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

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