# Guide: Automate a Python Script to Download CSV Daily on macOS This guide walks you through creating a Python script that downloads a CSV file daily, and scheduling it using `cron`. --- ## 📄 Step 1: Create the Python Script 1. Open **TextEdit**. 2. Go to **Format > Make Plain Text**. 3. Paste the following code: ```python import requests from datetime import datetime import os URL = "https://nycdob.github.io/ActiveShedPermits/data/Active_Sheds2.csv" SAVE_DIR = "downloads" os.makedirs(SAVE_DIR, exist_ok=True) today = datetime.now().strftime("%Y-%m-%d") filename = os.path.join(SAVE_DIR, f"active_sheds_{today}.csv") try: response = requests.get(URL) response.raise_for_status() with open(filename, 'wb') as f: f.write(response.content) print(f"[SUCCESS] Downloaded Active_Sheds2.csv as {filename}") except Exception as e: print(f"[ERROR] Failed to download file: {e}") ``` 4. Save the file as `download_active_sheds.py` in your **Downloads** folder. --- ## 💻 Step 2: Test the Script 1. Open **Terminal**. 2. Type: ```bash cd ~/Downloads python3 download_active_sheds.py ``` You should see a success message, and a file saved in a new `downloads` folder. --- ## ⏰ Step 3: Schedule the Script with cron 1. In Terminal, find the Python path: ```bash which python3 ``` It should show something like `/usr/bin/python3`. 2. Open your crontab for editing: ```bash crontab -e ``` If prompted, choose the **nano** editor. 3. At the bottom, add this line: ```bash 0 8 * * * /usr/bin/python3 /Users/YOUR_USERNAME/Downloads/download_active_sheds.py >> /Users/YOUR_USERNAME/Downloads/cron_log.txt 2>&1 ``` (Replace `YOUR_USERNAME` with your actual macOS username.) 4. Save and exit: - If using `nano`: Press `Control + O`, then `Enter`, then `Control + X`. --- ## ✅ Step 4: Confirm Scheduled Job Check your active cron jobs: ```bash crontab -l ``` You should see your job listed. --- ## 📝 Notes - Your Mac must be awake at the scheduled time for the cron job to run. - To test it, temporarily change the time in your cron job to 5 minutes from now. - Check `cron_log.txt` to verify it ran. You're done! 🎉 Your script will now run automatically every day.