2010-07-25 91 views
3

我使用os.statvfs來查找捲上的可用空間 - 除了查詢特定路徑的可用空間外,我希望能夠遍歷所有卷。目前我正在開發Linux,但理想情況是希望在Linux上返回["/", "/boot", "home"],在Windows上返回["C:\", "D:\"]如何枚舉Python的文件系統?

回答

3

對於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?