diff options
author | Eugeni Dodonov <eugeni@mandriva.com> | 2009-10-13 11:13:22 -0300 |
---|---|---|
committer | Eugeni Dodonov <eugeni@mandriva.com> | 2009-10-13 11:13:22 -0300 |
commit | 3db1778b8a92c6e6ea9dd66b72afb1b6e45ce203 (patch) | |
tree | f17fa8e01e647874eda79db8707b088c97c2fdcc /src | |
parent | 06b8f4d67808b4339b58f4f87281f590eeec8f1b (diff) | |
download | net_monitor-3db1778b8a92c6e6ea9dd66b72afb1b6e45ce203.tar net_monitor-3db1778b8a92c6e6ea9dd66b72afb1b6e45ce203.tar.gz net_monitor-3db1778b8a92c6e6ea9dd66b72afb1b6e45ce203.tar.bz2 net_monitor-3db1778b8a92c6e6ea9dd66b72afb1b6e45ce203.tar.xz net_monitor-3db1778b8a92c6e6ea9dd66b72afb1b6e45ce203.zip |
properly close open file descriptors
Diffstat (limited to 'src')
-rw-r--r-- | src/monitor.py | 21 |
1 files 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): |