Project structure
Organize your Python projects
When you write Python code, it's more than just a single file. You'll soon have many files working together. Let's learn how to keep everything neat and tidy.
Working with multiple files
As your programs grow, you'll want to break them into smaller, more manageable pieces. Each piece can live in its own file. This makes your code easier to read and maintain.
Imagine a recipe book. Instead of one giant page for everything, you have separate pages for appetizers, main courses, and desserts. Your Python project works similarly.
Organize your workspace
It's a good habit to create a new folder for each project. This keeps all your project's files together and separate from other projects on your computer.
mkdir my_first_project
cd my_first_projectThese commands create a new directory (folder) called my_first_project and then move you into it on the command line.
Create the data file
Let's start by creating a simple data file. This file will hold some information our Python script will use. We'll call it data.txt.
apple
banana
orangeSave these three lines inside data.txt within your my_first_project folder.
Understanding file paths
A file path tells your computer (and Python) exactly where a file is located. When a file is in the same folder as your script, you only need its name, like data.txt.
Create your first script
Now, let's create our Python script. We'll name it read_fruit.py. This script will open data.txt and print its contents.
def read_fruits_from_file(filename):
with open(filename, 'r') as file:
fruits = file.readlines()
return [fruit.strip() for fruit in fruits]
if __name__ == "__main__":
fruit_list = read_fruits_from_file("data.txt")
print("Here are your fruits:")
for fruit in fruit_list:
print(fruit)Save this code as read_fruit.py in the same my_first_project folder as data.txt.
Running your code
There are a couple of common ways to run your Python script. Let's look at them.
Option 1: The play button
If you're using an Integrated Development Environment (IDE) like VS Code or PyCharm, you'll often see a 'play' button. Clicking this button usually runs your current script.
This is great for quick tests, but it automatically handles some details that are good to understand manually.
Option 2: Interactive mode (recommended)
The most fundamental way to run Python code is through your terminal or command prompt. Navigate to your project folder first.
cd my_first_project
python read_fruit.pyThis command tells your computer to use the python interpreter to execute the read_fruit.py script. You should see the fruit list printed!
python3 instead of python, depending on how Python is installed on your computer.Best practices
- Always create a dedicated project folder for each project.
- Give your files clear, descriptive names.
- Keep related files (like
data.txtandread_fruit.pyin our example) in the same folder. - Learn to use your terminal to run scripts; it gives you more control.
Following these simple rules makes your projects much easier to manage, especially as they grow larger.