How to Shutdown your PC using Python ?

Β· 173 words Β· 1 minute read

If you want to shutdown your Windows PC from your Python code, just use this code snippet.

import os
os.system("shutdown /s /t 1")

Here’s a breakdown of what it does:

1. Import πŸ”—

  • import os: This line imports the os module, which provides functions for interacting with the operating system.

2. os.system("shutdown /s /t 1") πŸ”—

  • os.system: This function is similar to the system function in C. It takes a command string as an argument and executes it on the operating system.
  • "shutdown /s /t 1": This is the command string being passed to os.system. Let’s break it down further:
    • shutdown: This is a built-in command on most operating systems used for shutting down or restarting the computer.
    • /s: This flag instructs the computer to shut down.
    • /t 1: This flag specifies a timeout value in seconds. In this case, /t 1 sets the timeout to 1 second, which means the computer will shut down after 1 second.

That’s it. I hope this helps. Do you recommend reading this blog post? share it!

Share:
waffarx cash back