Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

How to get the free space values of disk drives

If you have a question or need help, this is the place to be.
Post Reply
M203
Posts: 3
Joined: Tue Dec 11, 2012 9:34 pm

How to get the free space values of disk drives

Post by M203 »

Hi,
i want to send information about available space on my three drives on my desktop (win 7) to my android devices.

So the trigger will be directory watcher noticing a file has been added, then I need the remaining space of that drive and send the value to my android devices.

So trigger is directory watcher and I send the data/values regarding remaining free space with Autoremote plugin for Eventghost but how do I get the disk space information?

Thanks
Peter
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: How to get the free space values of disk drives

Post by Pako »

Why do you not ask Uncle Google? He finds everything.
I did it and I got a page Disk usage (Python recipe).
I've tried it - it works.

Pako
M203
Posts: 3
Joined: Tue Dec 11, 2012 9:34 pm

Re: How to get the free space values of disk drives

Post by M203 »

Thanks!

I dont now where (python plugin?) to put the code or do I even know python. I do have friends who know python so hopefully they can help. And I guess I will have to search this site for documentation. Hopefully it i'll work out in the end. Thanks for the code!

BR
M203
User avatar
zian
Site Admin
Posts: 569
Joined: Wed Jun 24, 2009 3:54 pm

Re: How to get the free space values of disk drives

Post by zian »

Uncle Google.
lol

I LOVE that dude.
eventghost.net
Be there or be square.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: How to get the free space values of disk drives

Post by krambriw »

Just copy & paste the below into a python script and execute it

Code: Select all

## {{{ http://code.activestate.com/recipes/577972/ (r6)
#!/usr/bin/env python

"""
Return disk usage statistics about the given path as a (total, used, free)
namedtuple.  Values are expressed in bytes.
"""
# Author: Giampaolo Rodola' <g.rodola [AT] gmail [DOT] com>
# License: MIT

import os
import collections

_ntuple_diskusage = collections.namedtuple('usage', 'total used free')

if hasattr(os, 'statvfs'):  # POSIX
    def disk_usage(path):
        st = os.statvfs(path)
        free = st.f_bavail * st.f_frsize
        total = st.f_blocks * st.f_frsize
        used = (st.f_blocks - st.f_bfree) * st.f_frsize
        return _ntuple_diskusage(total, used, free)

elif os.name == 'nt':       # Windows
    import ctypes
    import sys

    def disk_usage(path):
        _, total, free = ctypes.c_ulonglong(), ctypes.c_ulonglong(), \
                           ctypes.c_ulonglong()
        if sys.version_info >= (3,) or isinstance(path, unicode):
            fun = ctypes.windll.kernel32.GetDiskFreeSpaceExW
        else:
            fun = ctypes.windll.kernel32.GetDiskFreeSpaceExA
        ret = fun(path, ctypes.byref(_), ctypes.byref(total), ctypes.byref(free))
        if ret == 0:
            raise ctypes.WinError()
        used = total.value - free.value
        return _ntuple_diskusage(total.value, used, free.value)
else:
    raise NotImplementedError("platform not supported")

disk_usage.__doc__ = __doc__

#if __name__ == '__main__':
print disk_usage(os.getcwd())

## end of http://code.activestate.com/recipes/577972/ }}}

M203
Posts: 3
Joined: Tue Dec 11, 2012 9:34 pm

Re: How to get the free space values of disk drives

Post by M203 »

Thanks Pako and Krambriw, the code you provied solved it. However, I ran into another problem.

I added a directory watcher and created a folder and dragged the event to my macro, added a python script with the code and finally an autoremote action to send the value to my android device. I execute the macro and it works fine all the way.

However I got an error message when saving my config. Also, I already have a couple of macros that have been running for a while now which wasnt a problem to save (sending message via autoremote to android when pc turns on / off. So I reloaded my working config and only added the directory watcher to my existing config and tried to save. I got the same error message as when trying to save the complete "send free disk to android" macro, please see below. What can be the problem? The log starts from where I added the directory watcher. The error indicates issue with autoremote but it has been fine to save before and the issue starts when adding just the directory watcher and then I try to save.

Code: Select all

Plugin: Directory Watcher
Error while saving file
Traceback (most recent call last) (1600):
  File "C:\Program Files (x86)\EventGhost\eg\Classes\Document.py", line 183, in WriteFile
    self.root.WriteXmlString(tmpFile.write)
  File "C:\Program Files (x86)\EventGhost\eg\Classes\TreeItem.py", line 128, in WriteXmlString
    self.WriteXmlChilds(streamWriter, newIndent)
  File "C:\Program Files (x86)\EventGhost\eg\Classes\RootItem.py", line 70, in WriteXmlChilds
    child.WriteXmlString(streamWriter, indent)
  File "C:\Program Files (x86)\EventGhost\eg\Classes\TreeItem.py", line 128, in WriteXmlString
    self.WriteXmlChilds(streamWriter, newIndent)
  File "C:\Program Files (x86)\EventGhost\eg\Classes\TreeItem.py", line 134, in WriteXmlChilds
    child.WriteXmlString(streamWriter, indent)
  File "C:\Program Files (x86)\EventGhost\eg\Classes\TreeItem.py", line 114, in WriteXmlString
    attr, text = self.GetData()
  File "C:\Program Files (x86)\EventGhost\eg\Classes\PluginItem.py", line 41, in GetData
    text = base64.b64encode(pickle.dumps(self.info.args, 2))
  File "pickle.pyc", line 1401, in dumps
  File "pickle.pyc", line 231, in dump
  File "pickle.pyc", line 293, in save
  File "pickle.pyc", line 569, in save_tuple
  File "pickle.pyc", line 293, in save
  File "pickle.pyc", line 607, in save_list
  File "pickle.pyc", line 640, in _batch_appends
  File "pickle.pyc", line 293, in save
  File "pickle.pyc", line 719, in save_inst
  File "pickle.pyc", line 293, in save
  File "pickle.pyc", line 765, in save_global
PicklingError: Can't pickle <class eg.CorePluginModule.AutoRemote.AutoRemoteDevice at 0x047FF8A0>: it's not the same object as eg.CorePluginModule.AutoRemote.AutoRemoteDevice
Thanks for your help
/P
Post Reply