Auto Shut Down Via Python

If you are given a task to shutdown your system then we can do it via python easily in just three lines of code.

Automation..

Let us see how:

We need to first import os..
Why?
It is a module in Python which provides us functions for creating and removing a directory, fetching its contents, changing and identifying the current directory.

Now import:
import os

Now let us invoke shutdown and then declare it.

shutdown = input("Do you want to shutdown? (yes/no): ")

Now invoke some looping statements:

if shutdown == 'yes':
os.system("shutdown /s /t 1")

Why 
os.system("shutdown /s /t 1")?
It is a method used to shutdown our laptop/PC using python script.
So we are using this function which make our work easy..

Now: 
Alternate loop.
else:
print('Shutdown is not required')

Now full code:

import os
shutdown = input("Do you want to shutdown the computer? (yes/no):  ")
if shutdown == 'yes':
    os.system("shutdown /s /t 1")
else:
    print('Shutdown is not required')

//output:




If yes then your system will shutdown automatically.

If no:

Scroll to top