Skip to content

Commit 962fba4

Browse files
committed
review file system
1 parent 3ffa4f3 commit 962fba4

File tree

1 file changed

+51
-44
lines changed
  • content/micropython/03.micropython/02.environment/02.file-system

1 file changed

+51
-44
lines changed

content/micropython/03.micropython/02.environment/02.file-system/file-system.md

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
title: 'File System'
33
description: 'Learn how to use the File Sytem in MicroPython.'
44
author: 'Pedro Lima'
5-
tags: [MicroPython, REPL]
5+
tags: [MicroPython, File System]
66
---
77

8+
When working with MicroPython, we’re not limited to a single program file like in traditional Arduino sketches (using C++). Instead, MicroPython provides a file system, enabling us to store and manage multiple files on our microcontroller. This opens up powerful capabilities for organizing code, managing assets, and creating modular projects.
89

9-
When working with MicroPython, we’re not limited to a single program like in traditional Arduino sketches. Instead, MicroPython provides a file system, enabling us to store and manage multiple files on our microcontroller. This opens up powerful capabilities for organizing code, managing assets, and creating modular projects.
10-
11-
In this article, we’ll explore how the MicroPython file system works, how to organize files effectively, and the typical structure of MicroPython projects.
10+
In this article, we'll explore how the MicroPython file system works, how to organize files effectively, and the typical structure of MicroPython projects.
1211

1312
## The MicroPython File System: Key Differences
1413

15-
In traditional Arduino programming, we upload a single compiled file directly to the microcontroller, where it runs immediately. With MicroPython, however, we work within a file system that can store multiple files. This file system allows us to:
14+
In traditional Arduino programming, we upload a single compiled file directly to the microcontroller, where it runs immediately. In MicroPython we work within a file system that can store multiple files. This file system allows us to:
1615

1716
- **Upload and Download Files**: We can save individual scripts, libraries, and assets directly on the device.
1817
- **Organize Project Files**: Create folders, save multiple scripts, and organize files for a modular approach.
1918
- **Edit Files Directly on the Device**: Modify files without needing to overwrite everything, making adjustments faster and more flexible.
19+
- **Run Different Scripts**: while we have `boot.py` (runs at start) and `main.py` (runs after start), we can also create more scripts that we can run, either from the editor, or from the board itself.
2020

2121
## Accessing the MicroPython File System
2222

@@ -26,9 +26,11 @@ To interact with the MicroPython file system, we’ll use Arduino Labs for Micro
2626
2. **Upload and Download Files**: Use the file manager to upload files from our computer to the microcontroller or download files back to our computer.
2727
3. **Organize Files**: We can create folders and store multiple files, making it easy to organize our project.
2828

29+
![Accessing the file system.]()
30+
2931
## Basic MicroPython File Structure
3032

31-
A typical MicroPython project includes a main file, boot script, libraries, and any supporting files our project needs. Here’s a standard layout:
33+
A typical MicroPython project includes a `main.py` file, `boot.py` file, libraries, and any supporting files our project needs. Here’s a standard layout:
3234

3335
```
3436
/ (Root Directory)
@@ -41,59 +43,64 @@ A typical MicroPython project includes a main file, boot script, libraries, and
4143
- **`boot.py`**: Runs once at startup, before `main.py`, and is typically used for system configurations that need to be set when the device first powers on.
4244
- **`main.py`**: This is the primary script, similar to the `setup()` and `loop()` structure in Arduino. It runs automatically after `boot.py` finishes.
4345

44-
## Example: Switching Execution from `main.py` to `favorite_things.py`
46+
## Example: Importing Code from Scripts
4547

46-
Let’s say we’re creating a project where `main.py` runs and, at a certain point, hands off control to `favorite_things.py` to perform a specific task. Here’s how we might organize it.
48+
With the MicroPython file system, we can create our own scripts and import them in the `main.py` script. This can be helpful in avoiding long scripts, as we instead can store the code in other files. This approach also makes it more modular.
4749

48-
1. **Write the Main Script** (`main.py`): This script runs some initial code and then switches to executing `favorite_things.py`.
49-
2.Create the file "favourite_things.py".
50-
3. **Add Content to `favorite_things.py`**: In this case, we’ll simply print a message from `favorite_things.py`.
50+
To run code from a separate script in our `main.py` file, we can follow the instructions below:
5151

52-
### Sample `main.py` Code
52+
1. Create a file named `my_new_script.py`, and add the following function:
5353

54-
Here’s how `main.py` might look, printing an introductory message and then executing `favorite_things.py`:
54+
```python
55+
def test():
56+
print("This runs from my_new_script.py")
57+
```
5558

56-
```python
57-
def main():
58-
print("Getting the list of my favourite things...")
59+
2. In `main.py`, we run some initial code and then switches to executing a function from `my_new_script.py`. Here's an example:
5960

60-
# Now execute favorite_things.py
61-
print("Switch to favorite_things.py...")
62-
try:
63-
with open("favorite_things.py") as f:
64-
exec(f.read())
65-
except OSError:
66-
print("Error: Could not open favorite_things.py")
67-
68-
if __name__ == "__main__":
69-
main()
70-
```
61+
```python
62+
import my_new_script
63+
print("This runs from main.py")
7164

72-
### Sample `favorite_things.py` Code
65+
my_new_script.test()
66+
```
7367

74-
In `favorite_things.py`, we’ll keep it simple with a message:
68+
3. Check the REPL, we should see:
7569

76-
```python
77-
# favorite_things.py
78-
print("Bears. Beets. Battlestar Galactica.")
79-
```
70+
```bash
71+
This runs from main.py # executed from main.py
72+
This runs from my_new_script.py #executed from my_new_script.py
73+
```
8074

81-
### Explanation
75+
Essentially, this is how [modules]() work. You import a module, and use a function from that module.
8276

83-
- **Switching Execution**: `main.py` starts by printing an introductory message, and then uses `exec(f.read())` to read and run the content of `favorite_things.py`.
84-
- **Running Another Script**: The `exec()` function allows `main.py` to execute `favorite_things.py` as if it were part of `main.py`, printing “Bears. Beets. Battlestar Galactica.” directly.
77+
![Import code from a script.]()
8578

86-
### Expected Output
79+
## Example: Directly Executing a Script
8780

88-
When you run `main.py`, the output should look like this:
81+
We can also directly execute another script stored on the device. For this example, let's create a script and name it `run_directly.py`.
8982

90-
```
91-
Getting the list of my favourite things...
92-
Switch to favorite_things.py...
93-
Bears. Beets. Battlestar Galactica.
94-
```
83+
1. In the script, store this code:
84+
85+
```python
86+
print("I was run from main.py")
87+
```
88+
89+
2. Then in `main.py`, we will use the `open()`, `exec()` and `read()` functions.
90+
91+
```python
92+
with open("run_directly.py") as f:
93+
exec(f.read())
94+
```
95+
96+
- `open()` - opens a file
97+
- `exec()` - executes a file
98+
- `read()` - reads a file
99+
100+
As a result, we should read `"I was run from main.py"` in the REPL. How this differs from the previous example, is that the `run_directly.py` script was just run from top to bottom, as opposed to importing a specific segment of code.
101+
102+
![Executing a script directly.]()
95103

96-
This setup demonstrates how to use MicroPython’s file system to organize and execute multiple scripts, allowing for modular and readable code.
97104

98105
## Organizing Code with Modules and Libraries
99106

0 commit comments

Comments
 (0)