2015-08-09 104 views
2

我正在为Kodi Media Center提供一个服务附加组件,它将检查剩余的磁盘空间并在空间低于500MB时提醒一个人使用维护工具已经创建。它作为一项单独的服务运行。我需要一种方法来使用Android上的python来确定剩余的磁盘空间。我尝试过使用statvfs(),但它显然只在包括OS X在内的类Unix系统上兼容。这意味着我可以在Linux和OSX中使用statvfs。我可以在Windows上使用wmi或ctypes,但目前为止还没有Android版本。我可以创建一个单独的包装来检查操作系统并为每个操作系统使用最好的方法 - 但是我找不到可以执行此操作的Android版python模块。有什么建议么?使用Python在Android上计算剩余磁盘空间

这里是我现有的代码:

import xbmc, xbmcgui, xbmcaddon 
import os, sys, statvfs, time, datetime 
from time import mktime 

__addon__  = xbmcaddon.Addon(id='plugin.service.maintenancetool') 
__addonname__ = __addon__.getAddonInfo('name') 
__icon__  = __addon__.getAddonInfo('icon') 

thumbnailPath = xbmc.translatePath('special://thumbnails'); 
cachePath = os.path.join(xbmc.translatePath('special://home'), 'cache') 
tempPath = xbmc.translatePath('special://temp') 
addonPath = os.path.join(os.path.join(xbmc.translatePath('special://home'), 'addons'),'plugin.service.maintenancetool') 
mediaPath = os.path.join(addonPath, 'media') 
databasePath = xbmc.translatePath('special://database') 


if __name__ == '__main__': 
    #check HDD freespace 
    st = os.statvfs(xbmc.translatePath('special://home')) 

if st.f_frsize: 
    freespace = st.f_frsize * st.f_bavail/1024/1024 
else: 
    freespace = st.f_bsize * st.f_bavail/1024/1024 

print "Free Space: %dMB"%(freespace) 
if(freespace < 500): 
    text = "You have less than 500MB of free space" 
    text1 = "Please use the Maintenance tool" 
    text2 = "immediately to prevent system issues" 

    xbmcgui.Dialog().ok(__addonname__, text, text1, text2) 


while not xbmc.abortRequested:  
    xbmc.sleep(500) 

这里是错误,我得到:

Error Type: <type 'exceptions.AttributeError'> 
Error Contents: 'module' object has no attribute 'statvfs' 
Traceback (most recent call last): 
File "/storage/emulated/0/Android/data/org.xbmc.kodi/files/.kodi/addons/plugin.service.maintenancetool/service.py", line 39, in <module> 
st = os.statvfs(xbmc.translatePath('special://home)) 
Attribute Error: 'module' object has no attribute 'statvfs' 
+0

试着用'os.popen()'来做到这一点,在那里你可以获得shell命令的响应。 – Gahan

回答

0

我已经找到了答案在kodi thread,应返回留在其余的字节android设备:

if xbmc.getCondVisibility('system.platform.android'): 
     import subprocess 
     df = subprocess.Popen(['df', '/storage/emulated/legacy'], stdout=subprocess.PIPE) 
     output = df.communicate()[0] 
     info = output.split('\n')[1].split() 
     size = float(info[1].replace('G', '').replace('M', '')) * 1000000000.0 
     size = size - (size % float(info[-1])) 
     available = float(info[3].replace('G', '').replace('M', '')) * 1000000000.0 
     available = available - (available % float(info[-1])) 
     return int(round(available)), int(round(size))