Get computer hardware parameters with python

        Hello friends! In the use of python, there are many standard libraries and many third-party libraries. Third-party libraries can help us do many things that standard libraries cannot do.

So my content today is to use the third-party library psutil library, platform library, shutil library and standard library time library to make a program for hardware acquisition.

The function of this program is to obtain the computer's memory, CPU, hard disk (only C drive), system version number, operating system digits and network name.

Before making the program, you need to install psutil, platform library and shutil library. In the cmd window, enter pip install psutil,

The pip install platform library and the pip install shutil library can run the code after the installation is complete.

Let's see the effect first

 

It can be seen that the program prints out the memory, CPU, hard disk and other information.

Next is the source code:

import psutil
import platform
import time
import shutil
print('----------------Welcome to the hardware acquisition system----------------')
time.sleep(1)
print('1. Memory information')
print('2.cpu information')
print('3. Hard disk information')
print('4. Other computer information')

while True:
    time.sleep( 1)
    print()
    ask1=input('Which information (serial number) do you want to obtain:')
    free = str(round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2))+'GB'
    total = str(round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2))+'GB' memory_use_percent
    = str(psutil.virtual_memory().percent)+' %'
    if ask1==' 1':
        print()
        print('Available memory:', free)
        time.sleep(1)
        print('total memory',total)
        time.sleep(1)
        print('memory usage',memory_use_percent)
        time.sleep(1)
        
    if ask1=='2':
        print()
        print( 'cpu usage', str(psutil.cpu_percent(interval=1))+' %')
        time.sleep(1)
        print('Physical cpu number',psutil.cpu_count(logical=False))
        time.sleep( 1)
        print("Your CPU information is:" + platform.processor())
        time.sleep(1)

    if ask1=='3':
        print()
        time.sleep(1)
        print('c drive:')
        total, used, free = shutil.disk_usage("/")
        time.sleep(1)
        print("Total : %d GiB" % (total // (2**30)))
        time.sleep(1)
        print("Used: %d GiB" % (used // (2**30)))
        time. sleep(1)
        print("Remaining space: %d GiB" % (free // (2**30)))
        time.sleep(1)
        
    if ask1=='4':
        print()
        print("Your system For: " + platform.system())
        time.sleep(1)
        print("Your operating system name and version number:" + platform.platform())
        time.sleep(1)
        print("Your operating system version number:" + platform.version())
        time.sleep(1)
        print("Get the number of bits of the operating system:" ,platform.architecture())
        time.sleep(1)
        print("The network name of the computer:" + platform.node())
        time.sleep( 1)

 That's it for the content of this article, thanks for reading this article, thank you.

Guess you like

Origin blog.csdn.net/hu20100913/article/details/126403017