使用Python打印网络共享驱动器中剩余的空间量

黑素

我想打印网络共享(M:驱动器)上的剩余空间(以GB为单位),然后采用该值并将其添加到Excel电子表格中。我对编程非常陌生,需要我能真正获得的所有帮助!

提前致谢

编辑:

到目前为止,这是我已经完成的工作。

import ctypes
from win32com.client import Dispatch
import pythoncom

def drivespace():
    #get space in bytes
    free_bytes = ctypes.c_ulonglong(0)
    ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(u'M:\\'), None, None ctypes.pointer(free_bytes))

    #calculate space in GB
    free_GB = free_bytes.value/1024/1024/1024
    print(free_GB)

    #input data to Excel
    xl = Dispatch ('Excel.Application')
    xl.visible = 0
    xl.Workbooks.Add (r'C:\Location\Location1\Location2\Book1.xlsm')
    xl.Run('Down1') #macro inside the workbook, just to move the cell down 1 row
    #here is where I need some help... something to input the data to the active cell
    #xl.Cells( ?? ACTIVE CELL HERE BUT DON'T KNOW HOW ?? ).value=(free_GB)
    xl.Quit()

    #release held Excel process
    pythoncom.CoUninitialize()

因此,基本上,除了将数据实际打印到活动单元中之外,我还进行了其他所有排序。有人有任何pywin32知识可以帮助我做到这一点吗?

谢谢一堆!

克里斯·普罗瑟

编辑后的评论

import ctypes, os, pythoncom
from win32com.client import Dispatch

def drivespace(drive, xl_path, col):
    #get space in bytes
    free_bytes = ctypes.c_ulonglong(0)
    ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(drive), \
                                               None, None, ctypes.pointer(free_bytes))

    #calculate space in GB
    free_GB = free_bytes.value/1024/1024/1024
    print(free_GB)

    #input data to Excel
    xl = Dispatch('Excel.Application')
    xl.visible = 0
    wb = xl.Workbooks.Open(xl_path)
    ws = wb.Worksheets(1)

    # initialise values
    empty = False
    row = 1

    # loop until first empty cell in this column
    while not empty:
        val = ws.Range(col+str(row)).value
        print val
        if val == None:
            print "Found first empty cell! Writing Value..."
            ws.Range(col+str(row)).value = free_GB
            empty = True
        row += 1

    wb.Close(True)
    xl.Quit()

    #release held Excel process
    pythoncom.CoUninitialize()

def main():
    drive = 'C:\\'
    xl_path = os.path.join(os.getenv('so'),'free_space_to_excel','Book1.xlsm')
    xl_column = 'A'
    drivespace(drive, xl_path, xl_column)

if __name__ == '__main__':
    main()

您只需要在主过程中更改值即可将其设置为驱动器,xl_path等。这需要对列字母进行额外的争论,然后找到该列中的第一个可用单元格。我认为这比在打开工作表时依靠特定单元格处于活动状态更为安全。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用网络共享作为驱动器的软件RAID

使用网络共享/驱动器创建本地库

通过网络共享驱动器

Python 2:从驱动器号获取网络共享路径

从公共网络共享驱动器中获取所有子文件夹

使用Powershell脚本通过本地网络共享驱动器

如何使用Linux终端映射驱动器网络共享?

Windows客户端使用位于网络共享驱动器上的.Net 4.0 exe的缓存副本

是否注销网络共享驱动器而没有重新启动?

将Windows安装程序文件夹移动到网络共享驱动器?

Windows 10网络共享到Android的外部驱动器

如何在Linux上自动为每个USB驱动器创建网络共享?

从 Docker 容器内的 NodeJS 连接到 Windows 网络共享驱动器

Windows 7:无法在资源管理器中浏览到网络共享,但是“映射网络驱动器”可以正常工作

是否可以查看远程服务器上的可用空间量而不必将其映射为网络驱动器?

与多个驱动器中的目录共享单个网络

在 Python 中确定 Windows 上共享驱动器的网络路径

使用“ xxx GB的yyy GB可用空间”栏将共享的网络驱动器添加到Windows

在VMware中扩展驱动器空间以供Linux使用

列出python中的驱动器和网络位置[Windows]

如何使用Google Drive API v3上传到Python中的共享驱动器?

无法使用网络共享

网络驱动器与“最喜欢”访问Windows桌面中的共享文件夹

使用凭据将I / O文件写入共享的网络驱动器

Windows 7无法使用共享的SAMBA凭据自动连接到SAMBA网络驱动器

使用 Azure AD 用户凭据将文件存储共享映射为网络驱动器

Virtual Box 共享文件夹与网络驱动器

无法连接到共享的网络驱动器

在网络上共享外部硬盘驱动器