Looking for Confirmation on My Python Script for Getting Computer Specs

0
1
Asked By TechieNerd42 On

Hey everyone! I'm trying to gather my computer specifications using a script without relying on any third-party software. I asked ChatGPT for help, but since I have zero coding knowledge, I want to ensure that the code it provided works properly.

Here's the code snippet I have:

```python
import platform
import psutil
import shutil
import subprocess
import os
import sys

def get_cpu_info():
cpu = platform.processor() or "Unknown"
freq = psutil.cpu_freq()
speed = f"{freq.max:.2f} MHz" if freq else "Unknown"
return cpu, speed

def get_ram():
ram = psutil.virtual_memory().total /(1024 ** 3)
return f"{ram:.2f} GB"

def get_gpu_info():
try:
if sys.platform == "win32":
cmd = "wmic path win32_VideoController get name,AdapterRAM"
output = subprocess.check_output(cmd, shell=True).decode()
lines = output.strip().split("n")[1:]
if lines:
name = lines[0].split()[0]
ram = int(lines[0].split()[-1]) /(1024 ** 2)
return name, f"{ram:.2f} MB"
elif sys.platform == "darwin":
output = subprocess.check_output(["system_profiler", "SPDisplaysDataType"]).decode()
lines = [line.strip() for line in output.split("n") if "Chipset Model" in line or "VRAM" in line]
name = lines[0].split(":")[-1].strip()
vram = lines[1].split(":")[-1].strip()
return name, vram
else: # Linux
output = subprocess.check_output("lspci | grep VGA", shell=True).decode()
name = output.strip().split(":")[-1].strip()
return name, "Unknown"
except Exception as e:
return "Unknown", "Unknown"

def get_os():
return f"{platform.system()} {platform.release()}"

def get_free_disk():
free = shutil.disk_usage("/").free /(1024 ** 3)
return f"{free:.2f} GB"

def get_shader_model():
if sys.platform == "win32":
try:
output = subprocess.check_output("dxdiag /t dxdiag_output.txt", shell=True)
with open("dxdiag_output.txt", "r", encoding="utf-8", errors="ignore") as file:
content = file.read()
os.remove("dxdiag_output.txt")
pixel = "Unknown"
vertex = "Unknown"
for line in content.splitlines():
if "Pixel Shader" in line:
pixel = line.split(":")[-1].strip()
if "Vertex Shader" in line:
vertex = line.split(":")[-1].strip()
return pixel, vertex
except:
return "Unknown", "Unknown"
else:
return "N/A", "N/A"

if __name__ == "__main__":
cpu, cpu_speed = get_cpu_info()
ram = get_ram()
gpu, vram = get_gpu_info()
os_info = get_os()
disk = get_free_disk()
pixel_shader, vertex_shader = get_shader_model()

print(f"CPU: {cpu}")
print(f"CPU SPEED: {cpu_speed}")
print(f"RAM: {ram}")
print(f"VIDEO CARD: {gpu}")
print(f"DEDICATED VIDEO RAM: {vram}")
print(f"PIXEL SHADER: {pixel_shader}")
print(f"VERTEX SHADER: {vertex_shader}")
print(f"OS: {os_info}")
print(f"FREE DISK SPACE: {disk}")
```

I would appreciate it if someone could review this code and confirm that there's nothing excessive or incorrect. Also, if there's a way to improve it, I'm all for it!

3 Answers

Answered By CuriousCoder92 On

You might want to double-check the handling of GPU info because it fetches differently based on the OS. Just ensure you run this on the intended OS and handle exceptions robustly. If everything checks out, it should do the job! Also, why not add a simple user input to specify OS in case it gets run on different systems? Just a thought!

Answered By GamerDude88 On

First off, this code is written in Python, not PowerShell! If you need a PowerShell script to get your specs, let me know. As for the Python code, it looks decent and should work for gathering basic specs, but you might want to ensure that you've got Python and necessary libraries installed. Just a heads up, do you have any specific specs you really need or just a general overview?

Answered By LostInCode91 On

The code looks okay overall, but since you mentioned you're looking to print the specs in a specific format, just make sure the output is neatly readable. For clarity, definitely consider wrapping the print statements in a function. This would make your overall setup a bit cleaner and easier to call when needed. Plus, if you're matching specs for games, you may want to account for more detailed GPU info depending on the games you intend to run.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.