<\/div><\/div>
What are automation scripts?<\/h1> Automation scripts are programs that perform repetitive tasks on your computer without needing your intervention each time. Think of them as little digital assistants designed to handle specific, routine jobs that you would otherwise do manually.<\/p>
It\u2019s a simple concept, but the payoff can be absolutely huge.<\/p>
For example<\/strong><\/p>Let\u2019s say you have a folder full of text files , and you need to rename all of them with a\u00a0.bak<\/code>\u00a0extension so that you can create backup copies. It’s a great habit to backup important data, but doing this manually would be tedious and time-consuming, especially if you had hundreds of files.<\/p>But with a Python automation script, you can instruct your computer to handle this task quickly and efficiently.<\/p>
Don\u2019t worry about the code too much for now, but here\u2019s a simple script that does exactly that:<\/p>
<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>import<\/span> os<\/span><\/span>\n<\/span>\nfor<\/span> filename <\/span>in<\/span> os.listdir(<\/span>'<\/span>.<\/span>'<\/span>):<\/span><\/span>\n <\/span>if<\/span> filename.endswith(<\/span>'<\/span>.txt<\/span>'<\/span>):<\/span><\/span>\n os.rename(filename, filename <\/span>+<\/span> <\/span>'<\/span>.bak<\/span>'<\/span>)<\/span><\/span><\/code><\/pre><\/div>So what\u2019s happening here?<\/p>
Basically, this script tells your computer to:<\/p>
Look at each file in the current directory (that\u2019s what the dot means)<\/li>\n\n Check if each file ends with\u00a0.txt<\/code><\/li>\n\nIf it does, rename it by adding\u00a0.bak<\/code>\u00a0to the end<\/li><\/ul>You don\u2019t need to understand each line of this script to use it. Just know that with Python, and a little bit of learning, you can turn repetitive tasks like this one into a thing of the past.<\/p>
There are even other tools out there that we can use alongside this such as GUI tools (more on these later) that can even simplify the code creation process.<\/p>
\n <\/path>\n<\/svg><\/div><\/div>The benefits of using Python automation<\/h1> You might be thinking, \u201c Ok this seems like a cool feature, but it\u2019s probably not worth my time to spend learning about<\/em> \u201c\u2026<\/p>Well, you would be wrong. Dead wrong. In fact, this slight detour to pick up this skill could save you literally hundreds of hours per year.<\/p>The benefits of using Python automation<\/figcaption><\/figure>Which brings me to benefit one on this list.<\/h4>#1. Automations save your time and money<\/h1> One of the main benefits of automation is that it saves time. Simply because, instead of performing the same tasks over and over again, you write a script once, and it does the work for you.<\/p>
You can then focus on more complex and creative tasks.<\/p>
For example<\/strong><\/p>I used to receive daily sales reports via email and needed to copy the data into a spreadsheet.<\/p>
This task took about 15 minutes each day, which doesn\u2019t seem like much<\/li>\n\n However, over a month, this added up to roughly 7.5 hours of dull, repetitive work<\/li>\n\n Over a year though that\u2019s 94 hours, or around 2 \u00bd weeks!<\/li><\/ul>But with a Python automation script, I could extract the data from these emails and automatically update the spreadsheet. Just a few clicks and I was done.<\/p>
Over a year, this saved me around 90 hours \u2014 a significant time saving that I could use for more important tasks or to learn new skills, like more advanced coding.<\/p>
Mind blown?<\/p>
Just think of the impact this could have:<\/p>
If you work for yourself, how much time and money could you be saving so that you\u2019re working on other important tasks?<\/li>\n\n If you work for a company, how much easier would your day job be if you were doing this?<\/li>\n\n Heck, what would be the impact if you did this for multiple repetitive tasks\u2026<\/li><\/ul><\/p>
#2. Automations reduce errors<\/h1> Mistakes happen, and even the most meticulous of us can mess up when performing repetitive tasks.<\/p>
However, Python automation removes the possibility of human error, ensuring that the task is performed accurately every time. The machine always does the same thing so zero mistakes and typos.<\/p>
#3. Automation enhances your productivity<\/h1> With the mundane tasks taken care of, you have more energy for tasks that truly need your attention and skills. This leads to better productivity and efficiency.<\/p>
#4. Python automations are fairly easy to learn<\/h1> Python\u2019s straightforward syntax is easy to pick up even for beginners. This makes it a popular choice for non-programmers who are looking to dip their toes into the world of coding.<\/p>
For example<\/strong><\/p>Let\u2019s look at the example script from earlier:<\/p>
<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>import<\/span> os<\/span><\/span>\n<\/span>\nfor<\/span> filename <\/span>in<\/span> os.listdir(<\/span>'<\/span>.<\/span>'<\/span>):<\/span><\/span>\n <\/span>if<\/span> filename.endswith(<\/span>'<\/span>.txt<\/span>'<\/span>):<\/span><\/span>\n os.rename(filename, filename <\/span>+<\/span> <\/span>'<\/span>.bak<\/span>'<\/span>)<\/span><\/span><\/code><\/pre><\/div>Notice how the code is kind of similar to normal language? You can probably guess what\u2019s happening on each line, but let\u2019s break it down.<\/p>
import os<\/code>\u00a0is kind of like saying “I need the operating system’s help for a task”<\/li>\n\nfor filename in os.listdir('.')<\/code>\u00a0is basically saying “for each file name in the current directory”<\/li>\n\nif filename.endswith('.txt')<\/code>\u00a0means “if the file name ends with ‘.txt'”<\/li>\n\nos.rename(filename, filename + '.bak')<\/code>\u00a0translates to “rename the file by adding ‘.bak’ to the end”<\/li><\/ul>Or simply put: Help me computer and do this task. Look at this directory. If the file ends with .txt<\/code>, change it to .bak<\/code>.<\/p>Better still, Python is also fairly easy to use, as well as understand.<\/p>
#5. You don\u2019t always have to start from scratch<\/h1> Python has an extensive range of \u2018libraries\u2019, but what does that mean?<\/p>
Well, in non-tech talk, these are basically databases of pre-written pieces of code that you can use to perform common tasks.<\/p>
This means you often won\u2019t have to start from scratch when automating a task. If there\u2019s something you want to do, there\u2019s probably a Python library that can help you do it.<\/p>
Give them a quick search and then test it out. Worst case scenario, you\u2019re half way to figuring out your task automation.<\/p>
#6. These automations work almost anywhere<\/h1> Python works on all major operating systems like Windows, macOS, and Linux.<\/p>
So, a Python automation script written on a Windows system will also work just as well on a Mac or Linux system with only a few modifications.<\/p>
I\u2019ll even show you how to use it on all systems later on.<\/p>
#7. You can always find help<\/h1> Python has a large and active community of developers. This means that if you ever run into problems or need to learn something new, there\u2019s a good chance that someone, somewhere, has already found a solution and shared it online.<\/p>
#8. Python is used everywhere<\/h1> Python\u2019s immense popularity in the programming world makes it an ideal choice for automation tasks, especially in collaborative or long-term projects.<\/p>
This is because when you write automation scripts in Python, you\u2019re using a language that countless other developers are familiar with. This becomes crucial when your scripts need to be maintained or modified by others in the future.<\/p>
#9. Python is a great entry point into a coding career<\/h1> Remember how I said we recommend Python to beginners? This is because it\u2019s used in so many industries, such as Machine Learning, AI, Data Science, and more.<\/p>
\n <\/path>\n<\/svg><\/div><\/div>Examples of Python automation scripts in action<\/h1> If you\u2019re still not sure if this could help you, let\u2019s look at a few more examples of potential python automations in real-life.<\/p>
Maybe you\u2019ll have an aha moment and suddenly realize you could streamline a lot of your own tasks.<\/p>
Example 1: Reading and writing files<\/h3> If your job involves handling large amounts of data, such as compiling reports or managing lists, automating file reading and writing can save you a lot of time.<\/p>
For example<\/strong><\/p>A marketing coordinator might need to compile weekly performance reports from multiple CSV files. Automating this task can save them several hours each week, reducing manual errors and ensuring timely updates.<\/p>
How it works:<\/strong><\/p><\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span># read_write_files.py<\/span><\/span>\n<\/span>\nwith<\/span> <\/span>open<\/span>(<\/span>'<\/span>input_file.txt<\/span>'<\/span>, <\/span>'<\/span>r<\/span>'<\/span>) <\/span>as<\/span> file:<\/span><\/span>\n data <\/span>=<\/span> file.read()<\/span><\/span>\n<\/span>\nwith<\/span> <\/span>open<\/span>(<\/span>'<\/span>output_file.txt<\/span>'<\/span>, <\/span>'<\/span>w<\/span>'<\/span>) <\/span>as<\/span> file:<\/span><\/span>\n file.write(data)<\/span><\/span><\/code><\/pre><\/div>This script reads data from input_file.txt and writes it to output_file.txt. You would need to customize the script with your actual file names and paths, but you get the idea.<\/p>
<\/p>
Example 2: Sending automated emails<\/h3> If your job involves sending regular updates or reports via email, automating this process can save you time.<\/p>
For example<\/strong><\/p>An office manager might need to send daily updates to their team but doesn\u2019t have any kind of automated email software that works internally to help with this.<\/p>
And so by automating this task, it ensures timely delivery and frees up your time for other tasks.<\/p>
How it works:<\/strong><\/p><\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span># send_email.py<\/span><\/span>\n<\/span>\nimport<\/span> smtplib<\/span><\/span>\nfrom<\/span> email.message <\/span>import<\/span> EmailMessage<\/span><\/span>\n<\/span>\nemail <\/span>=<\/span> EmailMessage()<\/span><\/span>\nemail[<\/span>'<\/span>from<\/span>'<\/span>] <\/span>=<\/span> <\/span>'<\/span>Your Name<\/span>'<\/span><\/span>\nemail[<\/span>'<\/span>to<\/span>'<\/span>] <\/span>=<\/span> <\/span>'<\/span>example@example.com<\/span>'<\/span><\/span>\nemail[<\/span>'<\/span>subject<\/span>'<\/span>] <\/span>=<\/span> <\/span>'<\/span>Daily Update<\/span>'<\/span><\/span>\n<\/span>\nemail.set_content(<\/span>'<\/span>