How to Schedule Python Scripts to Run Automatically (Windows & Mac) ⏰

Schedule Python Scripts

⚡ What You Will Learn

  • Windows Users: How to use “Task Scheduler” (No code required).
  • Mac/Linux Users: How to use “Crontab” (The hacker way).
  • The Goal: Make yesterday’s Downloads Cleaner Script run every morning at 9 AM.

Yesterday, we wrote a script that automatically cleans your Downloads folder. But there is a problem: You still have to click “Run” manually.

If you forget to run it, your folder gets messy again. That isn’t true automation.

True automation is “Set it and Forget it.” Today, we will show you how to schedule your Python scripts to run while you sleep.


Method 1: Windows Task Scheduler (The GUI Way) 🪟

Windows has a built-in tool that is perfect for this. It’s called Task Scheduler.

Step 1: Find Your Python Path

First, we need to know where Python is installed. Open your Command Prompt and type:

where python

Copy the path (e.g., C:\Users\You\AppData\Local\Programs\Python\Python39\python.exe).

Step 2: Create the Task

  1. Press the Windows Key and search for “Task Scheduler”. Open it.
  2. Click “Create Basic Task” on the right sidebar.
  3. Name: “Daily Downloads Cleaner”. Click Next.
  4. Trigger: Select “Daily”. Click Next.
  5. Action: Select “Start a Program”. Click Next.

Step 3: The Secret Settings (Important!) ⚠️

This is where most beginners mess up.

  • Program/script: Paste the path to your python.exe (from Step 1).
  • Add arguments: Paste the full path to your script (e.g., C:\Scripts\cleaner.py).
  • Start in: Paste the folder path where your script lives (e.g., C:\Scripts\).

Click Finish. Your script will now run every day at the time you picked!


Method 2: Mac & Linux Crontab (The Terminal Way) 🍎

Macs don’t have Task Scheduler. They have something better: Cron.

Step 1: Open Crontab

Open your Terminal and type:

crontab -e

This opens a text file where you list your scheduled tasks.

Step 2: Add Your Schedule

Cron uses a 5-star system to set time. To run your script every day at 9:00 AM, paste this line at the bottom of the file:

0 9 * * * /usr/bin/python3 /Users/You/Scripts/cleaner.py

Breakdown:

  • 0 9 * * * = “At minute 0 of hour 9 (9:00 AM)”.
  • /usr/bin/python3 = The path to Python.
  • /Users/.../cleaner.py = The path to your script.

Save the file (Press Esc, then type :wq and Enter). You are done!


Pro Tip: Hiding the “Black Window” 👻

On Windows, when the script runs, a black terminal window might pop up for a second. If that annoys you, use pythonw.exe instead of python.exe in Step 3.

pythonw runs scripts silently in the background. Invisible automation!


What’s Next?

Now your computer organizes itself automatically. You have officially built your first “System.”

Tomorrow, we are going to level up. We will learn how to connect Python to the internet to track data. Get ready for: “Build a Crypto Price Tracker Alert System.” 🚀


Frequently Asked Questions

Does the script run if my computer is turned off?

No. Your computer must be powered on for the script to run. However, if your computer is in “Sleep Mode,” Windows Task Scheduler can wake it up if you check the box that says “Wake the computer to run this task.”

How do I know if it actually ran?

Since the script runs silently, it’s hard to tell. A pro move is to add a simple “logging” line to your Python script. Add this line to the end of your code:

with open("log.txt", "a") as f:
    f.write("Script ran successfully!\n")

Now, you can just check log.txt to see a history of when it ran.

Can I schedule it to run every minute?

Yes! In Windows Task Scheduler, you can set the trigger to “Repeat task every X minutes.” In Mac/Linux Cron, you can use * * * * * to run it every single minute (though that might be overkill for organizing files!).

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top