Prevent File Not Found Errors with Python Script

Ever run a Python script and get a frustrating “file not found” error? 😤 This simple snippet can save you hours 👇 import os # Check if we're in the right place print("Current directory: ", os.getcwd()) # Check if our data file exists data_path = "data/sales.csv" if os.path.exists(data_path): print(f"Found {data_path}") else: print(f"X Cannot find {data_path}") print("Make sure you're running from the sales-analysis folder!") 💡 What’s happening here? 🔹 os.getcwd() Prints your current working directory — this tells you where your script is running from. Many errors happen because you're in the wrong folder. 🔹 data_path = "data/sales.csv" Defines the relative path to your dataset. 🔹 os.path.exists(data_path) Checks if the file actually exists before trying to use it. 🔹 Conditional check (if / else) Gives clear feedback: ✔ Found the file ❌ Or tells you it’s missing 🚀 Why this matters Prevents runtime errors Helps debug file path issues quickly Makes your scripts more reliable Essential habit for data analysis projects 📊 Whether you're working on data science, automation, or AI — always verify your file paths before processing data. Small habit. Big impact. #Python #Programming #DataScience #AI #CodingTips #Debugging

To view or add a comment, sign in

Explore content categories