How to Find Out your PC's Specs using Python

How to Find Out your PC's Specs using Python

Want to find out you PC's specs using Python? Well let's just delve in.

Arpan Pandey
·Dec 9, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • What all can we find out?
  • Let's get to installing
  • Importing the modules
  • Opening a file to write to
  • Get the size and convert.
  • The Basic Stuff
  • CPU Section
  • Memory Bits
  • Network Info
  • Closing the file
  • Wrapping Up...

What all can we find out?

This script can be used to find out practically all of the info you could need. Some of them are:

  • OS, host name etc.
  • CPU Info, RAM Info, SWAP Info
  • Disk Info
  • Network Information

Let's get to installing

We just need to install the psutil module. The other modules are pretty much pre-installed.

pip install psutil

Importing the modules

Now, we only need to import stuff in the file.

import psutil
import platform
from datetime import datetime

Opening a file to write to

We will now go on and open the file to write the data into.

f = open("sysinfo.txt", "w")

Get the size and convert.

We will now go on taking the data and converting them into an appropriate format.

def get_size(bytes, suffix="B"):
    Scale bytes to its proper format
        1253656 => '1.20MB'
        1253656678 => '1.17GB'
    factor = 1024
    for unit in ["", "K", "M", "G", "T", "P"]:
        if bytes < factor:
            return f"{bytes:.2f}{unit}{suffix}\n"
        bytes /= factor

The Basic Stuff

Now, we will go on to write the basic stuff like the the OS, Hostname, etc. But first we need to get the info using the platform library.

f.write(f'{"="*40, "System Information", "="*40}\n')
uname = platform.uname()
f.write(f"System: {uname.system}\n")
f.write(f"Node Name: {uname.node}\n")
f.write(f"Release: {uname.release}\n")
f.write(f"Version: {uname.version}\n")
f.write(f"Machine: {uname.machine}\n")
f.write(f"Processor: {uname.processor}\n")

CPU Section

In this section, we write stuff about the CPU like Cores, Frequency etc.

# Writing the CPU Information
f.write(f'{"="*40, "CPU Info", "="*40}\n')
# Writing the no. of cores
f.write(f'{"Physical cores:", psutil.cpu_count(logical=False)}\n')
f.write(f'{"Total cores:", psutil.cpu_count(logical=True)}\n')
# Writing CPU frequencies
cpufreq = psutil.cpu_freq()
f.write(f"Max Frequency: {cpufreq.max:.2f}Mhz")
f.write(f"Min Frequency: {cpufreq.min:.2f}Mhz")
f.write(f"Current Frequency: {cpufreq.current:.2f}Mhz")
# Writing CPU usage
f.write("CPU Usage Per Core:")
for i, percentage in enumerate(psutil.cpu_percent(percpu=True, interval=1)):
    f.write(f"Core {i}: {percentage}%")
f.write(f"Total CPU Usage: {psutil.cpu_percent()}%")

Memory Bits

This bit is about the memory info like Total Memory, Used, Available etc. It also check if any swap memory if present.

# Memory Information
f.write(f'{"="*40, "Memory Information", "="*40}\n')
# Getting the memory details
svmem = psutil.virtual_memory()
f.write(f"Total: {get_size(}\n")
f.write(f"Available: {get_size(svmem.available)}\n")
f.write(f"Used: {get_size(svmem.used)}\n")
f.write(f"Percentage: {svmem.percent}%")
f.write(f'{"="*20, "SWAP", "="*20}\n')
# Get the Swap Memory Details (If it Exists)
swap = psutil.swap_memory()
f.write(f"Total: {get_size(}\n")
f.write(f"Free: {get_size(}\n")
f.write(f"Used: {get_size(swap.used)}\n")
f.write(f"Percentage: {swap.percent}%")
# get IO statistics since boot
disk_io = psutil.disk_io_counters()
f.write(f"Total read: {get_size(disk_io.read_bytes)}\n")
f.write(f"Total write: {get_size(disk_io.write_bytes)}\n")

Network Info

This little snippet will give us any and all network interface whether they may be physical or virtual. Also, all of the I/O since boot.

# Network information
f.write(f'{"="*40, "Network Information", "="*40}\n')
# This snippet will get all network interfaces whether Physical or Virtual.
if_addrs = psutil.net_if_addrs()
for interface_name, interface_addresses in if_addrs.items():
    for address in interface_addresses:
        f.write(f"=== Interface: {interface_name} ===")
        if str( == 'AddressFamily.AF_INET':
            f.write(f"  IP Address: {address.address}\n")
            f.write(f"  Netmask: {address.netmask}\n")
            f.write(f"  Broadcast IP: {address.broadcast}\n")
        elif str( == 'AddressFamily.AF_PACKET':
            f.write(f"  MAC Address: {address.address}\n")
            f.write(f"  Netmask: {address.netmask}\n")
            f.write(f"  Broadcast MAC: {address.broadcast}\n")
# Getting the IO statistics since boot
net_io = psutil.net_io_counters()
f.write(f"Total Bytes Sent: {get_size(net_io.bytes_sent)}\n")
f.write(f"Total Bytes Received: {get_size(net_io.bytes_recv)}\n")

Closing the file

We need to close the file now to save everything.


Wrapping Up...

Cheers! You have reached the end of the article and hopefully followed along.

Did you find this article valuable?

Support Arpan Pandey by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
Share this