python - 我如何从 Python 中枚举文件系统?

标签 python windows linux filesystems

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

最佳答案

对于 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/

相关文章:

regex - 使用 "|"替代运算符进行 grepping

Python 3 通过发送 Ctrl C 停止子进程

python - 处理 WSGI 脚本时发生异常 - IOError : failed to write data

c++ - 从 Windows 10 的任务栏中删除窗口

使用 Visual Studio 创建大小为 100 字节的 C 程序

linux - 使用libuv处理unix fifos的方法有哪些?

java - Android Studio 中的最大堆大小无效错误

python nose xunit报告文件为空

python - mayavi:如何放大非常非常近(改变剪切距离附近)

c++ - Windows 与 fork() 最接近的东西是什么?