开发者

How can I enumerate filesystems from Python?

开发者 https://www.devze.com 2023-01-09 05:54 出处:网络
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.I\'m working on Lin

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. I'm working on Linux at the moment, but ideally would like something which returns ["/", "开发者_JAVA百科/boot", "home"] on Linux and ["C:\", "D:\"] on Windows.


For Linux how about parsing /etc/mtab or /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:

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消