How can I list file systems from Python?

I use os.statvfsto find out the free space available on a volume - besides requesting free space for a specific path, I would like to be able to iterate over all volumes. I am currently working on Linux, but ideally I would like something that returns ["/", "/boot", "home"]on Linux and ["C:\", "D:\"]on Windows.

+3
source share
1 answer

For Linux, what about parsing /etc/mtabor /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 it, maybe they will help.

It should also help: Is there a way to list all available drive letters in python?

+3

Source: https://habr.com/ru/post/1756392/


All Articles