One thing that as a python programmer you should be proud of is that it gives you the capability to automate a bunch of repetitive tasks effortlessly just with the basics.
In this article, you’re going to learn about building a cross-platform python program for automating shutdown.
In this article, we gonna use OS + Platform to implement our script, both of them comes with the python standard library utilities so you don’t need to install anything
Basics of Platform Module
Platform Module, we will use it to determine the type of Operating system in which our program is running.
Example of usage
>>> import platform >>> platform.system() 'Linux'
If you’re using windows you should see Windows instead of Linux.
Basics of OS Module
OS Module , we are going to use this module to run terminal/command prompt command in Python.
To run run the system command in Python we are going to use system ( )
Example of Usage
>>> import os >>> os.system('echo hello') hello 0
The command for shutdown computer varies depending on the operating system .
System command for automated shutdown
Below a system command for automating computer shutdown, our task gonna lie on running programmatically do this based on user input from python.
shutdown command for Linux
shutdown -t time_in_minutes
kalebu@kalebu-PC:~$ shutdown -t 12 Shutdown scheduled for Wed 2020-06-03 15:55:40 EAT, use 'shutdown -c' to cancel.
cancelling automated shutdown
kalebu@kalebu-PC:~$ shutdown -c
shutdown command for Window
shutdown -s -t time_in_seconds
shutdown -s -t 1200
Cancelling scheduled shutdown in Window
shutdown -s -t a
Building our Python program.
Therefore we need to get the time parameter to automate the shutdown from the user and then run those command from our Python program.
import os import platform OS_type = platform.system() OS_type = OS_type.lower() def shutdown(time): ''' It automate shutdown by scheduling the time for computer to shutdown ''' if OS_type == 'linux': shutdown_str = 'shutdown -t '+ str(time) elif OS_type == 'windows': time = time * 60 shutdown_str == 'shutdown -s -t ' + str(time) else: print('Sorry this feature is not available in ', OS_type) return os.system(shutdown_str) def cancel_shutdown(): if OS_type == 'linux': cancel_str = 'shutdown -c' elif OS_type == 'windows': cancel_str = 'shutdown -a' else: return os.system(cancel_str) def main(): print('Use Shutup To schedule your shutdown'.center(50, '=')) print('1.Automate Shutdown\n2.Cancel shutdown') option = int(input('Option here: ')) if option == 1: time = int(input('\nEnter time to shutdown in Minutes : ')) shutdown(time) elif option == 2: cancel_shutdown() print('Shutdown successful canceled ...') else: print('Invalid option try again!!!\n') main() if __name__ == '__main__': main()
I recommend you to also check these, based on your interest;
- How to build a website blocker in Python
- How to save your disk space with Python
- How to generate Qr-code in Python
- How to track Coronavirus in Python
- How to build a simple chatbot in Python
Hope you find this post interesting , don’t forget to subscribe to get more tutorials like this. Full code available on My Github
In case of any suggestion or comment , drop it on the comment box and I will reply to you immediately.