This is a basic introduction to scripting and automation. After introducing these topics and explaining why Python should be the go to tool for these types of tasks, I'll show some code examples.
What is a Script?
I want to bring this up because when I started researching about automation, I went into it coupling the idea of scripting and automation together, and I didn't even really know why. When I really started to understand what a script actually is, I understood why it is fundamental for anything automated with code. They are the recipes that set up your robot cook to be able to start cooking.
A script is really just a focused piece of code that does something. For example, it can convert a large data sample into a readable report for you. It could take an email template and a list of recipients and send them all a personalized email. It can convert all the images in a specific file to jpegs. You get the idea. Then, chain scripts together to get more complex outputs/functionality and schedule them to run without needing human input; now you're automating!
What made this all so confusing to me at first was figuring out the difference between a programming language and a scripting language. The difference isn't hugely important in the context of this blog except that scripting is much easier in languages considered "scripting languages" like JavaScript or Python.
Cool, now that we understand what a script is; let's give our robot cook some recipes, so it can start cooking.
What is Automation?
Let's say I'm the head chef of a brand new startup food truck. The opening was rushed, so the menu is really small. I want time for R&D so I can expand the menu, but my truck's popularity is really starting to pick up. I don't have enough time on my hand! This is the core concept behind why we automate. When we can offload menial tasks to our machine it frees up two of our most precious resources: time and mental capacity. If I had a robot cook to operate my fryers automatically, I'd declutter my mind to have better connections with my customers and have time to R&D new menu items while we're slow! Also, my new robot is probably a better fry cook than even me. He never messes up his timers!
Okay, that was a pretty niche example; I know, so let's take a look at some more tangible ones if you're still not seeing how powerful this could be for you. I can give all my employees key cards to login, automating security checks and what they have access to on an individual level. I can automatically scrape large customer data sets and monitor who my best customers are. Maybe even give them a promo discount. I could even automate a portion of my paycheck to go straight into savings when I get paid. The list goes on.
So, Why Python?
Interaction with Operating System
Scripts being run in the terminal and modules like os, sys, and shutil let Python touch system files, settings and OS easily.
Extensive Library
The list of available packages that help with automation are massive. This makes automating tasks related to data science, web scraping, web browsing, file management etc. very easy.
Ease of Access
Python is fast to write and easy to understand, saving developers time and getting scripts up and running quickly. This is especially useful in fields like cybersecurity where counter-attacks need to launch as soon as possible.
The Ecosystem
This is the way that it developed over time. JavaScript was built around web design, and its ecosystem reflects that. Python was built around being easy and fast to write, and it shows. The frameworks available for it are diverse and powerful, ranging from web development to science to game development to security. As time goes on, people will keep pushing the general purpose uses of Python even further.
Example Time
So, hopefully I've opened you're eyes to why Python is an amazing tool for automation. Let's look at what it might look like practically.
This a script written by Devesh Tiwari and Josh Vinson. You can find the link to the github repository here. I chose this example because it shows how easily Python scripts can touch system files. It isn't really automated, but it is a really good example of a type of script that Python might excel in over JavaScript. If we hooked this up to happen whenever a large folder system gets added to my computer, that would be automating it.
import os
import shutil
from datetime import datetime
def categorize_file(filepath):
filename, extension = os.path.splitext(filepath)
categories = {
".jpg": "images",
".png": "images",
".pdf": "documents",
".docx": "documents",
".mp4": "videos",
".avi": "videos",
".mkv": "videos",
".exe": "apps"
}
return categories.get(extension.lower(), "other")
def create_folders(directory):
os.makedirs(os.path.join(directory, "images"), exist_ok=True)
os.makedirs(os.path.join(directory, "documents"), exist_ok=True)
os.makedirs(os.path.join(directory, "videos"), exist_ok=True)
os.makedirs(os.path.join(directory, "other"), exist_ok=True)
os.makedirs(os.path.join(directory, "apps"), exist_ok=True)
def sort_files(source_directory, target_directory):
for root, _, files in os.walk(source_directory):
for filename in files:
filepath = os.path.join(root, filename)
file_category = categorize_file(filename)
target_path = os.path.join(target_directory, file_category, filename)
try:
shutil.copy2(filepath, target_path)
file_date = datetime.fromtimestamp(os.path.getmtime(filepath))
# Replace colons to avoid invalid filename errors
sanitized_date = file_date.isoformat().replace(":", "-")
new_filename = f"{sanitized_date}-{filename}"
os.rename(target_path, os.path.join(os.path.dirname(target_path), new_filename))
except Exception as e:
print(f"Error moving '{filepath}': {e}")
def main():
source_directory = input("Sorry to bother you Lord Vinson, but I'll need the source directory:")
target_directory = input("Sorry to bother you once again Lord Vader, but I'll need the destination directory:")
create_folders(target_directory)
sort_files(source_directory, target_directory)
print("File organization completed!")
if __name__ == "__main__":
main()
The script takes an input target file path and destination file path. It then sorts them by what type of file they are, and copies them over to the new destination. It also prepends the date and time that they were moved to the file name. One thing to note about this script is that it makes copies of the original files and does not delete them, so this is a great script if you wanted to export a folder but keep them how they are on your machine.
Automating things is easier than ever. The are so many free tools available for pretty much anything you can think of with Python. I encourage you to do some more research on this topic if you found this interesting. Here are some good resources to get you started:
Top comments (0)