Automating and Scheduling Tasks Using Python

This article Automating the boring Stuff: Scheduling Tasks using Python

This article Automating the boring Stuff: Scheduling Tasks using Python

Python is an interpreted, high-level, cross-platform, and easy-to-use programming language. Currently, it is one of the most powerful programming languages used in back-end web development, software development, artificial intelligence, machine learning, data processing, image processing, automation etc. We can automate almost everything on our system using the Python programming language. In this article, we will automate and schedule some tasks on our system using python.

Why Automating Tasks

In real life, there are some tasks that are not too difficult but time taking and it feels too much hard to complete those tasks manually. For example, renaming hundreds of files in a directory manually, organizing files in a directory, creating a backup of your files regularly, etc. These tasks are really time taking and you can save that much time by automating these tasks. There are many similar examples and we can make them easier by automating them. Saving time, increasing efficiency and increasing accuracy are the main reasons to automate tasks.

Important Python Automation Modules

There are a lot of automation modules in python. We can use these modules to automate almost everything on our system. We can use the ‘os’ module to manipulate our system like creating or deleting directories or files etc. System commands can also be run using this module. ‘Pyautogui’ is a module used to automate tasks related to keyboard and mouse. We can find screen resolution, move the pointer to a specific point, press any key, even drag and drop files using this module. Similarly, we can use the ‘time’ module to manipulate almost everything related to time. We can convert date and time to any format and use it in our program. ‘PIL’ is the module used to process images in python. We can read, crop, rotate, flip, and change the size of images using the ‘PIL’ module. We can even change the color scheme of an image from RGB to black and white using this module. In this article, we will discuss some use cases of automation using python which will involve some of the above modules.

Automating Tasks Using Python

In this section of the article, we will discuss some use cases in which python is used to automate some tasks on your system.

Automatically Deleting Files that are not Accessed for Last 30 Days

Let you have a lot of files in your downloads directory. Some of them are really important and you access them every two or three days and out of them, some are unnecessary. You want to delete those unnecessary files. This task will be easy and can be done manually if you have five to ten files in the downloads directory. But what if you have hundreds of files in your downloads directory? It will be a very difficult and time taking process to delete unnecessary files manually. You can make this process easy by automating this task.

We will automate this task in such a way that all those files that are not accessed for the last thirty days will be deleted and the remaining files will remain the same. First of all, open python by typing the following command in the terminal,

Now import required modules and go to the downloads directory using the ‘os’ module

Now save a list of all the files and folders present in the downloads directory in a variable called lists.

Get the current time and also find the number of seconds in thirty days.

Now we will check each file one by one whether it is a file or a directory. If the item is a directory then we will take no action on it and if the item is a file then we will check when it was accessed last time. If the file was accessed within the last thirty days, then we will take no action on it otherwise we will delete it.

The above code, whenever you run, will automatically check all the files in the downloads directory and remove those files that are not accessed for the last thirty days.

NOTE: Use this function with care as files removed will not go to the recycle bin and hence can not be restored.

Organizing Files in Download Directory Using System Commands in Python

In this section of the article, we will automate another task and for this task, we will use system commands in python instead of using python’s commands. We will use system commands of Ubuntu and if you are using Windows operating system then some commands will be a little bit different.

Let you have a lot of files in your download directory and you want to organize them in such a way that all the files with the same extension like (.pdf .docx etc) go to a single directory. Open python by typing the following command in the terminal,

First of all, import the required module and go to download directory,

Now note all the files and directories in a list named ‘files’

Now check each item whether it is a file or a directory. Skip all the directories and for each file, check the extension and move it to the corresponding directory. If the corresponding directory does not exist, then create a directory then move the file to the directory

Scheduling Python Scripts Using Crontab

So far we have written scripts to automate some tasks using python. Now What about executing python scripts automatically? Python scripts will not be executed automatically, we will have to execute them manually. We can schedule python scripts to execute automatically after a specific time using crontab.

If we want these python scripts to run automatically every week so we will schedule it using crontab as follows. Open crontab in editing mode by typing the following command in the terminal,

It will open a file and put the following line in the bottom of the file

The first entry in the above line is for minutes and it ranges from 0 to 59. The second entry is for hours which ranges from 0 to 23. The third entry is used to describe the day of the month and its range is from 1 to 31. The fourth entry is used to specify the month and its range is from 1 to 12. We can also use JAN, FEB, etc for the month. The fifth and the last entry is used to describe the day of the week and its range is from 0 to 6 with Sunday being 0. We can also specify the day of the week by using short names of the day like SAT, SUN, etc.

After these entries come to the command which we want to run. In this case, we want to schedule our python scripts so we will run the above command. The Python script used in this cron job will run every week at 00:00. You can further get a deeper knowledge about scheduling tasks using crontab by visiting the following link https://crontab.guru/.

Automating and scheduling your tasks can make life easier by saving time and increasing efficiency. In this article, we have used different use cases of automating tasks using python. We discussed two examples of automation in this article but you can use this knowledge to automate some other tasks. It is not everything but you can get the idea of automation using a programming language. You can also automate anything using system commands in python.

 

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

27.07.2023

Creating Our Own Chat GPT

In June, OpenAI announced that third-party applications' APIs can be passed into the GPT model, opening up a wide range of possibilities for creating ...

12.06.2023

The Ultimate Guide to Pip

Developers may quickly and easily install Python packages from the Python Package Index (PyPI) and other package indexes by using Pip. Pip ...

Building a serverless web application with Python and AWS Lambda

AWS Lambda is a serverless computing solution that enables you to run code without the need for server provisioning or management. It automatically ...

2 comments

ali ikhlaq October 25, 2020 at 8:33 pm
0

Good information keep it up

Lokesh Kumar November 2, 2020 at 9:39 am
0

Thanks for sharing very informative article with us..

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy