Shutdown any computer using Python in 4 lines of code - Python project simple


Today we will learn new python projects and tricks in which we will shut down a pc with 4 lines of code. 


To start  first we will create a new python file called shut.py:


The code for the project >> shut.py



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

shutdown_PC()

Let's understand the code:



  • import os

    • import os - It is preinstalled in python libraries. So you have no need to reinstall it using the pip command. It’s an operating system.

  • define shutdown pc

def shutdown_PC():

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

# run this command  to shut down the system


  • shutdown_PC() # call this function

  • And finally, pc is shutdown using shutdown_PC()


To shut down your computer with python, ensure that you save and close all running files. make sure you have saved and closed all files except your code editor where you wrote the python program to shut down your system. As soon as your program starts running, your system will shut down in the next few seconds.


The final step is the output:


 (base) C:\Users\HP\Desktop\Projects>python shut.py


Conclusion:

So this is how you can easily shut down your system by using the Python programming language. The OS module in Python is a very useful tool as it can be used in many more tasks that are completely dependent on your system’s operating system. 


I hope you like this python small trick. I have been posting lots of python projects with the source code for python beginners. 


Follow us on social media as well to get more such amazing ideas, mini projects, and all the study material for python, machine learning, artificial intelligence, data science, etc.


Post Tags
python projects for beginners     python projects with source code     python projects simple    

About the author

Aaquib  khan
Aaquib khan

Aaquib is a passionate entrepreneur, Data Science enthusiast, and technical writer. He is also a experienced software developer. He loves traveling and learning new things.