Working with multiple files
So far, you’ve been writing single Python files. But real projects have multiple files, folders, and resources. Good organization makes your code easier to understand and maintain. Let’s organize your existingpython-for-ai workspace with a proper structure.
Organize your workspace
You already have apython-for-ai workspace from the beginning of the course. Let’s create a folder for this project:
- Open your
python-for-aiworkspace in VS Code - Create a
sales-analysisfolder - Inside
sales-analysis, create these subfolders:
Create folders in VS Code by right-clicking in the Explorer panel and selecting “New Folder”, or click the new folder icon at the top of the Explorer.
Create the data file
Inside yoursales-analysis/data folder, create a file called sales.csv:
Copy and paste this data into a new file called
sales.csv in your data folder. Make sure the file extension is .csv, not .txt.Understanding file paths
When your code needs to find files, you need to understand paths:Create your first script
In thesales-analysis folder (not in a subfolder), create analyzer.py:
Running your code
In VS Code, you have two simple ways to run Python code:Option 1: The play button
- Open
analyzer.pyin VS Code - Click the ▶️ (play) button in the top right
- The code runs and shows output in the terminal below
Option 2: Interactive mode (recommended)
- Open
analyzer.pyin VS Code - Select the code you want to run (or place cursor on a line)
- Press Shift+Enter
- Code runs in the interactive window with instant feedback
Interactive mode (Shift+Enter) is great for testing code step by step. You can run one line at a time and see results immediately!
By keeping
analyzer.py in the same folder as data and output, the file paths stay simple and work with both the play button and interactive mode.Best practices
- Keep data separate - Don’t mix code and data files
- Use clear names -
datafor input files,outputfor results - Simple structure - Keep your main script at the project level
- Test as you go - Use Shift+Enter to run code step by step
Python paths
Understand how Python finds files