Skip to content

Instantly share code, notes, and snippets.

@dasbluehole
Created August 31, 2022 15:42
Show Gist options
  • Save dasbluehole/e007affd9e63625c4a47f825a9e72943 to your computer and use it in GitHub Desktop.
Save dasbluehole/e007affd9e63625c4a47f825a9e72943 to your computer and use it in GitHub Desktop.
CPU and Memory information using python on Linux
#cpu info mem info by ashok.s.das@gmail.com
#reads and parses /proc/cpuinfo and /proc/meminfo to obtain information
def parse_cpu_info():
info=dict()
try:
f=open('/proc/cpuinfo','r')
except IOError:
return{}
while True:
line = f.readline()
#print(line,len(line))
if not line or len(line)<=1: break
pair = line.split(":")
info[pair[0].strip()]=pair[1].strip()
print('CPU is',info['vendor_id'],'model',info['model name'], 'having',info['cpu cores'],'cores')
f.close()
def mem_info():
minfo=dict()
try:
f=open('/proc/meminfo','r')
except IOError:
return{}
while True:
line = f.readline()
#print(line,len(line))
if not line or len(line)<=1: break
pair = line.split(":")
minfo[pair[0].strip()]=pair[1].strip()
print('Total Memory =',minfo['MemTotal'],'free',minfo['MemFree'],'Available',minfo['MemAvailable'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment