os.popen在一段时间后返回空

时间:2018-02-15 06:03:11

标签: python operating-system openwrt

我的项目中有一个函数,它返回/ tmp分区大小和可用的数量,它有时会运行良好,但是在一段时间之后os.popen函数返回空文件对象,函数将抛出错误。以下是我的代码。

def get_temp_dir_size():
    """ the function returns the tmp partetion size and avilable size"""
    data = [0,0]
    with os.popen('df -h /tmp') as f:
        f_data = f.readlines()
        try:
            data = re.findall(r'(\d*\.*\d+)M',f_data[-1])
        except:
            log.exception("*** Some error occured")
            log.debug("*** data in the file : {0}".format(f_data))
    return {'flashSize':data[0],'flashFree':data[1]}

以下是错误

Traceback (most recent call last):
  File "/etc/dnp/dnp_utils.py", line 614, in get_temp_dir_size
    data = re.findall(r'(\d*\.*\d+)M',f_data[-1])
IndexError: list index out of range
2018-02-15 05:51:36,626 [DEBUG](           dnp_utils: 617): *** data in the file : []

0 个答案:

没有答案
相关问题