有谁知道通过 Python 2.6 及其标准库获取 Windows (Samba) 共享上可用空间量的方法?(也在 Windows 上运行)
例如
>>> os.free_space("\\myshare\folder") # return free disk space, in bytes
1234567890
如果PyWin32可用:
free, total, totalfree = win32file.GetDiskFreeSpaceEx(r'\\server\share')
其中free是当前用户可用的可用空间量,totalfree是可用空间总量。相关文档:PyWin32 文档、MSDN。
如果不保证 PyWin32 可用,那么对于 Python 2.5 及更高版本,stdlib 中有ctypes 模块。相同的功能,使用 ctypes:
import sys
from ctypes import *
c_ulonglong_p = POINTER(c_ulonglong)
_GetDiskFreeSpace = windll.kernel32.GetDiskFreeSpaceExW
_GetDiskFreeSpace.argtypes = [c_wchar_p, c_ulonglong_p, c_ulonglong_p, c_ulonglong_p]
def GetDiskFreeSpace(path):
if not isinstance(path, unicode):
path = path.decode('mbcs') # this is windows only code
free, total, totalfree = c_ulonglong(0), c_ulonglong(0), c_ulonglong(0)
if not _GetDiskFreeSpace(path, pointer(free), pointer(total), pointer(totalfree)):
raise WindowsError
return free.value, total.value, totalfree.value
可能会做得更好,但我对 ctypes 不是很熟悉。
标准库有 os.statvfs() 函数,但不幸的是它只在类 Unix 平台上可用。
如果有一些 cygwin-python 也许它会在那里工作?