【发布时间】:2010-06-04 11:03:21
【问题描述】:
有谁知道通过 Python 2.6 及其标准库获取 Windows (Samba) 共享上可用空间量的方法? (也可以在 Windows 上运行)
例如
>>> os.free_space("\\myshare\folder") # return free disk space, in bytes
1234567890
【问题讨论】:
有谁知道通过 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 docs、MSDN。
如果不保证 PyWin32 可用,那么对于 Python 2.5 及更高版本,stdlib 中有 ctypes module。相同的功能,使用 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 也许它会在那里工作?
【讨论】: