I feel there must be an easier (cleaner) way to use comprehensions to parse the meminfo file on linux. The format of the file is:
MemTotal: 3045588 kB
MemFree: 1167060 kB
Buffers: 336752 kB
Cached: 721980 kB
SwapCached: 0 kB
Active: 843592 kB
Inactive: 752920 kB
Active(anon): 539968 kB
Inactive(anon): 134472 kB
I tried to rewrite the for loop id been using to use a comprehension and found I needed 3 of them…
def parse_mem_file(memfile = '/proc/meminfo'):
lines = open(memfile, 'r').readlines()
lines = [line.strip('kB\n') for line in lines if line[-3:] == 'kB\n']
lines = [line.split(':') for line in lines]
return dict((key, int(value)) for (key, value) in lines)
print parse_mem_file()
What am I doing wrong? Is there a reasonable way to simplify this?
1 Answer