Blog Posts

5 Tasks To Automate With Python

Blog: Think Data Analytics Blog

Here are 5 tasks you can automate with Python, and how to do it.

You automate. I automate. We all automate. We automate our finances, our to-do lists, and our social lives.

Why then, is there still so much resistance to automating our professional lives? I’ve been a software engineer for over a decade, and I’ve been an automation advocate for just as long. I’ve seen the benefits of automation firsthand and have helped companies adopt it. In this blog post, I’ll share 10 small tasks that you can automate with Python.

Introduction

 
Whether you are writing software, writing business logic, or simply taking notes, automation is your friend.

The software world has been fighting an “artificial intelligence arms race” with our competitors for a long time now. Even Google is working on autonomous robots. How can we, as developers, compete? By focusing on our own strengths.

We can do this by applying the same techniques that we use for product development to software development.

We can apply advanced techniques to our problem-solving and then automate collecting information to be used in those solutions. I personally find that the greater the depth of the problem I solve, the easier it is for me to become a master at the solution and to then specialize in the parts of the problem I find most interesting.

5 Tasks to Automate with Python

 
This is by no means a comprehensive list, nor will it provide the same level of detail for each task. But it should give you a solid starting point. If you’re new to automation, I recommend checking out the Robot Academy archive to learn more.

#1. Reading (Turn any file into an audiobook)

 
You can turn any file on your Mac into an audiobook with the script below, and listen to it in the background.

First, install the following dependency.

pip install mac-say

Then create a python file you will be using to execute this task.

import sysimport mac_saymac_say.say(["-f", sys.argv[1], "-v", "Alex"])

Then in the command line just point at a file of your choice, and enjoy

python audiobook.py fileofyourchoice.txt

#2. Quick Weather Reports

 
Checking the weather is usually a quick thing, but there can be a bit of satisfaction, by doing it with a click of a button.

This as well only requires a single dependency.

pip install requests

Once installed just create a file to run with the script below.

import sysimport requestsresp = requests.get(f'https://wttr.in/{sys.argv[1].replace(" ", "+")}')
print(resp.text)

After that, you are ready to run or schedule each day the following.

python weather.py "Your City"

#3. Converting Currencies

 
This one is a bit easier all we need to do is to install the library as below.

pip install --user currencyconverter

This installation should put currency_converter in our $PATH so to execute a conversion one just needs to write the following as shown in the example execution.

currency_converter 1 USD --to EUR

#4. Automatically Sorting Your Downloads Folder

 
In this example, we will just listen for PDFs, images, audio, and video, but this can be expanded quite a bit and should be enough to get you started. I went a little overboard with this one.

import os
import time
import sysfrom watchdog.observers import Observer
from watchdog.events import FileSystemEventHandlerfolder_to_monitor = sys.argv[1]file_folder_mapping = {
    '.png':'images',
    '.jpg':'images',
    '.jpeg':'images',
    '.gif':'images',
    '.pdf':'pdfs',
    '.mp4':'videos',
    '.mp3':'audio',
    '.zip':'bundles'
}class DownloadedFileHandler(FileSystemEventHandler):
    def on_created(self, event):
        if any(event.src_path.endswith(x) for x in file_folder_mapping):
            parent = os.path.join(os.path.dirname(os.path.abspath(event.src_path)), file_folder_mapping.get(f".{event.src_path.split('.')[-1]}"))
            if not os.path.exists(parent):
                os.makedirs(parent)
            os.rename(event.src_path, os.path.join(parent, os.path.basename(event.src_path)))event_handler = DownloadedFileHandler()
observer = Observer()
observer.schedule(event_handler, folder_to_monitor, recursive=True)
print("Monitoring started")
observer.start()
try:
    while True:
        time.sleep(10)except KeyboardInterrupt:
    observer.stop()
    observer.join()

Once you have the file created for this, all you need to do is to run it pointing at your downloads directory to start monitoring it.

python downloads-watchdog.py "/your/downloads/folder"

#5. The Morning Setup Script

 
In the morning usually, you want to do very little until the caffeine hits. This script will get your morning started earlier by opening all of the browser tabs you usually need to open each morning. Save a script file with URLs of your choice as shown in the example below.

python -m webbrowser -t "https://www.google.com"
python -m webbrowser -t "https://www.dylanroy.com"
python -m webbrowser -t "https://www.usesql.com"

Conclusion

 
Python is a powerful tool, but the more you learn and practice it, the more efficient and productive you’ll become. It has been my pleasure to share some silly or fun automation tasks with you, and I hope that you found them useful. If you have any questions, feel free to ask.

Resources

original

The post 5 Tasks To Automate With Python appeared first on ThinkDataAnalytics.

Leave a Comment

Get the BPI Web Feed

Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.

Copy/Paste this code in your website html code:

<iframe src="https://www.businessprocessincubator.com/content/5-tasks-to-automate-with-python/?feed=html" frameborder="0" scrolling="auto" width="100%" height="700">

Customizing your BPI Web Feed

You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.

Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:

Customizing the Content Filter

Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site:

Get the BPI Web Feed

Some integration examples

BPMN.org

XPDL.org

×