From 3db1778b8a92c6e6ea9dd66b72afb1b6e45ce203 Mon Sep 17 00:00:00 2001 From: Eugeni Dodonov Date: Tue, 13 Oct 2009 11:13:22 -0300 Subject: properly close open file descriptors --- src/monitor.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/monitor.py b/src/monitor.py index 4c7802b..9d96d14 100644 --- a/src/monitor.py +++ b/src/monitor.py @@ -90,9 +90,8 @@ class Monitor: def get_status(self, ifname): try: - fd = open("/sys/class/net/%s/operstate" % ifname) - status = fd.readline().strip() - fd.close() + with open("/sys/class/net/%s/operstate" % ifname) as fd: + status = fd.readline().strip() except: status="unknown" if status == "unknown": @@ -145,12 +144,16 @@ class Monitor: def readnet(self): """Reads values from /proc/net/dev""" net = {} - data = open("/proc/net/dev").readlines()[2:] - for l in data: - dev, vals = l.split(":") - dev = dev.strip() - vals = vals.split() - net[dev] = vals + try: + with open("/proc/net/dev") as fd: + data = fd.readlines()[2:] + for l in data: + dev, vals = l.split(":") + dev = dev.strip() + vals = vals.split() + net[dev] = vals + except: + traceback.print_exc() return net def get_traffic(self, iface, net=None): -- cgit v1.2.1