python - SMB 共享上的可用磁盘空间,通过 Python

标签 python windows samba

有谁知道通过 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 及更高版本,有 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 不是很熟悉。

关于python - SMB 共享上的可用磁盘空间,通过 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2973480/

相关文章:

c++ - 记录第二个键盘事件

windows - 您可以在 WinPE 环境中将哪些编程语言/平台用于 GUI?

linux - 使用 sed 命令插入一行

python - 使用单个函数更改 tkinter 中多个检查按钮的值

python - 在 Python 中聚类文本

python kivy 多个标签

linux - Samba 身份验证和 Linux 用户映射

Python:将查询字符串分解为关联数组不起作用

windows - 将参数传递到 Azure ARM 模板

linux - 映射 samba 共享时网络使用错误 1312