Explore featured Python scripts, tools, and tutorials crafted for Linux environments.
Browse Python Code
import os
import shutil
from datetime import datetime
src_folder = "/home/user/projects"
backup_folder = f"/home/user/backups/backup-{datetime.now().strftime('%Y%m%d')}"
shutil.copytree(src_folder, backup_folder)
print("Backup completed.")
import psutil
import time
while True:
print("CPU Usage:", psutil.cpu_percent(), "%")
time.sleep(2)
import psutil
def get_cpu_temperature_psutil():
try:
temperatures = psutil.sensors_temperatures()
if "coretemp" in temperatures: # Linux specific, adjust for other os
for entry in temperatures["coretemp"]:
if "CPU" in entry.label or "Package" in entry.label:
return entry.current
elif "cpu_thermal" in temperatures: # Raspberry Pi specific
for entry in temperatures["cpu_thermal"]:
return entry.current
else:
# Fallback for other systems of if specific labels aren't found
# you might need to inspect "temperatures" to find the correct key/label
for sensor_type, sensor_list in temperatures.items():
for sensor in sensor_list:
return sensor.current
print("CPU temperature data not found via psutil in common locations.")
return None
except AttributeError:
print("psutil.sensors_temperatures() not avalible on this system or platform")
return None
except Exception as e:
print(f"An error occured: {e}")
return None
cpu_temp = get_cpu_temperature_psutil()
if cpu_temp is not None:
print(f"CPU Temperature: {cpu_temp} °C")
linux-dev.com is dedicated to developers building for Linux platforms using Python, Bash, and open tools. Our mission is to empower coders with practical scripts and real-world use cases.
Got code to share or questions?
[email protected]