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