🐍 A 5-Minute Python Script That Would Take 11 Hours in Corporate Development

Tom Smykowski
5 min readNov 8, 2024

--

Tired of the endless grind of coding “by the book”? This quick and scrappy Python utility saves time by automating tedious tasks, and I knocked it out in just five minutes. For those willing to bend the rules of “perfect” code, here’s a practical approach that actually gets the job done — fast.

And if you’re looking for tools to speed up your learning process, my Python Flashcard Deck is made just for that. With sci-fi, fantasy, and neutral styles, this deck makes mastering Python not only faster but more fun. You can grab it as a physical deck or an even more affordable printable.

I’m still not posting regularly on Medium. As I’ve mentioned, it’s unsustainable and doesn’t really yield passive income. I wrote daily for two years, and now, my earnings from those articles are close to zero. It feels like once you stop writing, all your past work fades into oblivion.

But if you’re a Python coder looking for a solid, no-nonsense learning tool that won’t disappear, you might want to check out my Python deck — it’s got everything you need to cover the essentials without the filler. You can even get it in various languages for global accessibility.

Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away ― Antoine de Saint-ExupĂ©ry

So, maybe it’s time to take those articles off Medium? Who knows. On the bright side, I get a steady stream of troll and spam comments to keep me entertained. For now, I’m not feeling particularly inspired to write, but I do have something to share. Maybe it’ll resonate with those who are tired of endlessly fiddling with divs and pixels.

Here’s a small Python utility script that makes my life easier:

import time
import os
import subprocess

# Configuration
files_to_watch = ["instruction.py", "instruction_side_printer.py"] # Files to monitor for changes
irfanview_file = "side.png" # File to open in IrfanView
adobe_reader_file = "instruction.pdf" # File to open in Adobe Reader
use_irfanview = False # Set to False to use Adobe Reader instead of IrfanView

# Paths to IrfanView and Adobe Reader
irfanview_path = r"C:\Program Files\IrfanView\i_view64.exe"
adobe_reader_path = r"C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe"

# Functions
def file_modified_time(file_path):
try:
return os.path.getmtime(file_path)
except FileNotFoundError:
print(f"File {file_path} not found.")
return None

def close_irfanview():
print("Attempting to close IrfanView...")
result = subprocess.run("taskkill /f /im i_view64.exe", shell=True, capture_output=True, text=True)
if result.returncode == 0:
print("IrfanView closed successfully.")
else:
print("Failed to close IrfanView. Output:", result.stderr)

def close_adobe_reader():
print("Attempting to close Adobe Reader...")
result = subprocess.run("taskkill /f /im Acrobat.exe", shell=True, capture_output=True, text=True)
if result.returncode == 0:
print("Adobe Reader closed successfully.")
else:
print("Failed to close Adobe Reader. Output:", result.stderr)
time.sleep(2) # Wait briefly to ensure Adobe Reader fully closes

def open_file_in_irfanview():
print(f"Opening {irfanview_file} in a new IrfanView instance...")
subprocess.Popen([irfanview_path, irfanview_file])

def open_file_in_adobe():
print(f"Opening {adobe_reader_file} in Adobe Reader...")
subprocess.Popen([adobe_reader_path, adobe_reader_file])

def run_instruction_script():
retry_count = 5
for attempt in range(retry_count):
try:
print("Executing main script:", files_to_watch[0])
subprocess.run(["python", files_to_watch[0]], check=True)
print("Script executed successfully.")
break
except PermissionError as e:
print(f"Attempt {attempt + 1} - Permission denied for 'instruction.pdf'. Retrying...")
time.sleep(1) # Wait before retrying
except Exception as e:
print(f"An unexpected error occurred: {e}")
break

# Initialize modification times for each file to watch
last_modified_times = {file: file_modified_time(file) for file in files_to_watch}

# Main loop to monitor files
if all(last_modified_times.values()):
print(f"Watching {', '.join(files_to_watch)} for changes...")

while True:
try:
# Check for changes in any of the files being watched
changed = False
for file in files_to_watch:
current_modified_time = file_modified_time(file)
if current_modified_time and current_modified_time != last_modified_times[file]:
print(f"Change detected in {file}. Running script...")
last_modified_times[file] = current_modified_time
changed = True

if changed:
# Ensure viewer is closed before running the script
if use_irfanview:
close_irfanview()
else:
close_adobe_reader()

# Run the main script (instruction.py) with retry on PermissionError
run_instruction_script()

# Open or refresh the file based on viewer preference
if use_irfanview:
open_file_in_irfanview()
else:
open_file_in_adobe()

# Wait a short time before checking again
time.sleep(1)

except Exception as e:
print(f"An error occurred: {e}")
else:
print("Error: One or more files to watch do not exist.")

This script keeps an eye on two Python files that create an instructional PDF. The instruction_side_printer.py script stitches pages together, and then instruction.py generates the final PDF. Normally, testing changes in these files means repeatedly generating new images and PDFs, and opening them in IrfanView and Adobe Reader to confirm everything looks right. Tedious, right?

This script saves me from that repetitive hassle. It detects any changes in the files, runs the relevant script, waits for the PDF or image to generate, and then opens it. It also closes previous instances of IrfanView and Adobe Reader to keep windows from piling up.

So now, I just update the script, save, and see the results on my second screen instantly.

Speaking of efficiency, my Python flashcards work the same way: they streamline essential Python concepts into bite-sized learning tools, saving you hours. Unlike sifting through heavy books or lengthy tutorials, this deck offers you key insights in a matter of seconds. It’s perfect for quick study sessions on the go or to give you a coding boost before an interview.

Here’s how they stack up:

How to Use the Deck: Simply order your favorite style, and keep it handy while coding. Or, if you’re a printable fan, print the PDF for easy on-screen flipping. It’s practical and portable, whether you’re coding at home or on the go.

Is this utility script perfect? Does it follow best practices? Not exactly — there’s even a time.sleep(1) loop in there. But it gets the job done, and that’s what matters to me. If you're a Python programmer, you might have already scrolled down to the code, but if you stuck with me this far, kudos. As someone who’s half indie, half corporate developer, I know when to prioritize function over form. Sometimes, you just need the right tool to make things click.

Wonder how long a corporate developer would spend writing this script with all best practices in mind? I’d guess around 11 hours. It took me 5 minutes, which means I was about 132 times faster. That’s the power of efficient tools, and my Python decks are designed with the same mindset: practical, straightforward, and focused on speed.

For those asking “Is it affordable?” Yep. The physical decks are great to have, but if you’re on a budget, grab the cheaper printable version you can print on your own and start coding your way in style. Available in multiple languages, my decks bring Python to everyone. Don’t just code — experience Python.

I like my code like I like my coffee — quick, effective, and without 11 hours of corporate meetings to plan it :)

Cheers!

Let me know if you’re team Sci-Fi, Fantasy, or Neutral in the comments below!

--

--

Tom Smykowski
Tom Smykowski

Written by Tom Smykowski

Software Engineer & Tech Editor. Top 2% on StackOverflow, 3mil views on Quora. Won Shattered Pixel Dungeon.

Responses (5)