Python to Shut Down/Restart Your Computer with Full Source Code For Beginners

This script shuts down or restarts your computer

Run the Script:

Steps on how to run the script along with suitable examples.

  1. Type the following on the command line: python
  2. Press enter and wait for prompt. Type “r” to restart or “s” to shut down

Example: python Use ‘r’ for the restart and ‘s’ for shutdown: r

Source Code:

import os import platform def shutdown(): if platform.system() == "Windows": os.system('shutdown -s') elif platform.system() == "Linux" or platform.system() == "Darwin": os.system("shutdown -h now") else: print("Os not supported!") def restart(): if platform.system() == "Windows": os.system("shutdown -t 0 -r -f") elif platform.system() == "Linux" or platform.system() == "Darwin": os.system('reboot now') else: print("Os not supported!") command = input("Use \'r\' for restart and \'s\' for shutdown: ").lower() if command == "r": restart() elif command == "s": shutdown() else: print("Wrong letter")
Code language: PHP (php)

Leave a Comment