1. System Resource usage
- Libraries Used: psutil
- import psutil
- def get_system_info():
- # Get CPU utilization
- cpu_percent = psutil.cpu_percent()
- print(f"CPU utilization: {cpu_percent}%")
- # Get battery information
- battery = psutil.sensors_battery()
- print(f"Battery charge: {battery.percent}%")
- # Get RAM utilization
- virtual_memory = psutil.virtual_memory()
- print(f"RAM utilization: {virtual_memory.percent}%")
- # Get available memory
- available_memory = virtual_memory.available / virtual_memory.total * 100
- print(f"Available memory: {available_memory:.2f}%")
- get_system_info()
- CPU utilization: The function uses psutil.cpu_percent() to get the current CPU utilization as a float value, which is then formatted as a string and printed to the console using an f-string.
- Battery information: The function uses psutil.sensors_battery() to get information about the battery, and then prints the battery charge percentage, which is represented by the percent attribute of the returned object.
- RAM utilization: The function uses psutil.virtual_memory() to get information about the system's virtual memory, and then prints the RAM utilization percentage, which is represented by the percent attribute of the returned object.
- Available memory: The function calculates the available memory by dividing the available attribute of the psutil.virtual_memory() object by the total attribute, and multiplying by 100 to get the result in percentage. This value is then formatted as a string with two decimal places and printed to the console.
- Finally, the get_system_info function is called at the end of the code, which executes the code and displays the results on the console.
- Disk usage: You can use psutil.disk_usage('/') to get information about disk usage, including total, used, and free space. You can then print this information to the console.
- Network information: You can use psutil.net_io_counters() to get information about the network I/O (input/output) activity, such as bytes sent and received.
- Boot time: You can use psutil.boot_time() to get the time when the system was last booted, which you can then format as a string and print to the console.
- Logical disk partitions: You can use psutil.disk_partitions() to get information about the logical disk partitions on the system, including the device, mount point, file system type, and other information.
- Process information: You can use psutil.process_iter() to iterate over all running processes and get information about each process, such as its name, status, memory usage, and CPU utilization.
- # Get disk usage information
- disk_usage = psutil.disk_usage('/')
- print(f"Disk total: {disk_usage.total / (1024 ** 3):.2f} GB")
- print(f"Disk used: {disk_usage.used / (1024 ** 3):.2f} GB")
- print(f"Disk free: {disk_usage.free / (1024 ** 3):.2f} GB")
- # Get network information
- net_io_counters = psutil.net_io_counters()
- print(f"Bytes sent: {net_io_counters.bytes_sent / (1024 ** 2)} MB")
- print(f"Bytes received: {net_io_counters.bytes_recv / (1024 ** 2)} MB")
- # Get boot time
- boot_time = psutil.boot_time()
- boot_time_str = datetime.datetime.fromtimestamp(boot_time).strftime('%Y-%m-%d %H:%M:%S')
- print(f"System boot time: {boot_time_str}")
2. Website All Link Extractor
- Libraries Used: requests,bs4
- import requests
- from bs4 import BeautifulSoup
- # Make a request to the website
- url = "https://www.learnpython.org/"
- page = requests.get(url)
- # Parse the HTML content of the page
- soup = BeautifulSoup(page.content, "html.parser")
- # Find all the anchor tags
- links = soup.find_all("a")
- # Extract the href attribute from each anchor tag
- for link in links:
- href = link.get("href")
- print(href)
3. Show Saved WiFi Password
This is a script written in Python that retrieves the saved Wi-Fi profiles and their passwords on a Windows machine using the subprocess module. The netsh command is used to retrieve the profiles and the profile information.
- Libraries Used: subprocess
- import subprocess
- data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8', errors="backslashreplace").split('\n')
- profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]
- for i in profiles:
- try:
- results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i, 'key=clear']).decode('utf-8', errors="backslashreplace").split('\n')
- results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]
- try:
- print ("{:<30}| {:<}".format(i, results[0]))
- except IndexError:
- print ("{:<30}| {:<}".format(i, ""))
- except subprocess.CalledProcessError:
- print ("{:<30}| {:<}".format(i, "ENCODING ERROR"))
- input("")
4. Shortcut key with python
- Libraries Used: pyautogui ,time
- import pyautogui
- from time import sleep
- # 1. Change tab : Wait for 5sec and switch
- pyautogui.keyDown("alt")
- pyautogui.press("tab")
- sleep(5)
- pyautogui.keyUp("alt")
- # 1.1 Direct change Tab
- # pyautogui.hotkey('alt','tab')
- # 2.Close current app
- pyautogui.hotkey('alt', 'f4')
- # 3.Open setting
- pyautogui.hotkey('winleft','i')
- # 4.Open the hidden menu
- pyautogui.hotkey('winleft','x')
- # 5.Open the Task Manager
- pyautogui.hotkey('ctrl','shift','esc')
- # 6.Open the Task view
- pyautogui.hotkey('winleft','tab')
- # 7.TAKE Screenshot
- pyautogui.hotkey('winleft','prtscr')
- # 8.TAKE SNIP
- pyautogui.hotkey('winleft','shift','s')
- # 9.Add a new virtual desktop
- pyautogui.hotkey('winleft','ctrl','d')
- # 10. open system properties
- pyautogui.hotkey('winleft','r')
- pyautogui.typewrite("sysdm.cpl\n", interval=0.1)
5. Notepad Automation
- Libraries Used: pyautogui, time
- import pyautogui
- from time import sleep
- # Open Notepad
- pyautogui.hotkey('winleft')
- pyautogui.typewrite('notepad\n', interval=0.5)
- # Wait for Notepad to open
- sleep(2)
- # Type some text in Notepad
- pyautogui.typewrite("Hello, this is automated text from codesempai", interval=0.1)
- # Save the file
- pyautogui.hotkey('ctrl', 's')
- # Wait for the Save As dialog to appear
- sleep(2)
- # Type the file name and press Enter
- pyautogui.typewrite("sample.txt\n", interval=0.1)
- # Close Notepad
- pyautogui.hotkey('alt', 'f4')
6. Youtube Automation
- Libraries Used: pyautogui
- import pyautogui as pg
- def open_chrome_and_visit_youtube():
- # Open the start menu
- pg.hotkey('winleft')
- # Type "chrome" and press Enter
- pg.typewrite('chrome\n', interval=0.5)
- # Type "www.youtube.com" and press Enter
- pg.typewrite('www.youtube.com\n', interval=0.2)
- # Move the focus to the Chrome window and maximize it
- pg.hotkey('winleft', 'up')
- # Open a new tab in Chrome
- pg.hotkey('ctrl', 't')
- if __name__ == "__main__":
- open_chrome_and_visit_youtube()
- It opens the Windows start menu using the pg.hotkey('winleft') function.
- It types the word "chrome" in the search bar and presses Enter using the pg.typewrite('chrome\n', interval=0.5) function. The interval argument specifies the time in seconds between keypresses.
- It types the URL "www.youtube.com" in the address bar of the Chrome browser and presses Enter using the pg.typewrite('www.youtube.com\n', interval=0.2) function.
- It moves the focus to the Chrome window and maximizes it using the pg.hotkey('winleft', 'up') function.
- It opens a new tab in the Chrome browser using the pg.hotkey('ctrl', 't') function.
7. Change wallpapers!
- Libraries Used: ctypes,os,random
- import ctypes
- import os
- import random
- def set_random_wallpaper(dir):
- afolder = os.listdir(dir)
- folder = dir + "/" + random.choice(afolder)
- print(folder)
- ctypes.windll.user32.SystemParametersInfoW(20, 0, folder, 3)
- set_random_wallpaper("D:/Wallpapers/best of")
The set_random_wallpaper function takes a directory as an argument, which is assumed to contain subdirectories of wallpapers. The function first lists the contents of the directory and selects a random subdirectory. The path of the selected subdirectory is then passed as an argument to SystemParametersInfoW along with the appropriate parameters to set it as the desktop background.
Note that the script assumes that the subdirectories in the specified directory contain wallpapers and that the wallpapers are in a format supported by Windows (e.g. JPEG, BMP, etc.). Additionally, the script assumes that the path to the directory is correctly specified and that the user running the script has the necessary permissions to change the desktop background.
8. Test Internet Speed
- Libraries Used: speedtest
- import speedtest
- speed_test = speedtest.Speedtest()
- def bytes_to_mb(bytes):
- KB = 1024 # One Kilobyte is 1024 bytes
- MB = KB * 1024 # One MB is 1024 KB
- return int(bytes/MB)
- print("please wait for few seconds...")
- download_speed = bytes_to_mb(speed_test.download())
- print("Your Download speed is", download_speed, "MB")
It defines a function called "bytes_to_mb" to convert the download speed from bytes to megabytes. It then prints a message to the user to wait for a few seconds, measures the download speed using the speed_test object, converts the result to megabytes using the bytes_to_mb function, and prints the download speed to the user in megabytes.
9. Get Computer specs
- Libraries Used: wmi
- import wmi
- try:
- computer = wmi.WMI()
- computer_info = computer.Win32_ComputerSystem()[0]
- os_info = computer.Win32_OperatingSystem()[0]
- proc_info = computer.Win32_Processor()[0]
- gpu_info = computer.Win32_VideoController()[0]
- os_name = os_info.Name.encode('utf-8').split(b'|')[0].decode()
- os_version = f"{os_info.Version} {os_info.BuildNumber}"
- system_ram = float(os_info.TotalVisibleMemorySize) / 1048576 # KB to GB
- print(f"OS Name: {os_name}")
- print(f"OS Version: {os_version}")
- print(f"CPU: {proc_info.Name}")
- print(f"RAM: {system_ram:.2f} GB")
- print(f"Graphics Card: {gpu_info.Name}")
- except Exception as e:
- print(f"Error: {e}")
10. Countdown Timer
- Libraries Used: Time, sys
- import time
- import sys
- def countdown(t): #defineing the function name countdown
- while t > 0:
- sys.stdout.write('\rDuration : {}s'.format(t))
- t -= 1
- sys.stdout.flush()
- time.sleep(1)
- countdown(10) #calling the function
- The sys.stdout.write method is used to display the current time in seconds remaining,
- while sys.stdout.flush is used to ensure that the output is displayed immediately instead of being buffered.
- Finally, the time.sleep function is used to pause the program for one second between each iteration of the loop.
- import time
- import sys
- def countdown(t: int) -> None:
- for i in range(t, 0, -1):
- sys.stdout.write('\rDuration : {}s'.format(i))
- sys.stdout.flush()
- time.sleep(1)
- print()
- countdown(10)
- The sys.stdout.write method is used to write the current countdown value to the standard output, which is typically the terminal.
- The sys.stdout.flush method is used to force the standard output to be immediately updated, so the countdown value appears correctly on the same line, rather than being printed on multiple lines.
- The time.sleep(1) call is used to pause the execution of the program for 1 second so that the countdown timer progresses at a rate of 1 second per iteration.