Learn how to handle files and directories with the Raspberry Pi Pico programmed with MicroPython. We’ll cover how to create and perform operations with files and directories saved permanently on the Raspberry Pi Pico filesystem.
The filesystem lets you access the Raspberry Pi Pico flash memory like you would on your computer to access files and folders. You can read, write, create, and delete files.

Learning this subject is useful to create files to save configurations, save variables’ values permanently (even after resetting the Raspberry Pi Pico), log data or sensor values in a file that you can use later, and much more.
Prerequisites – MicroPython Firmware
To follow this tutorial, you need MicroPython firmware installed on your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.
The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.
If you’re still getting started with the Raspberry Pi Pico, follow one of these getting-started guides:
- Getting Started with Raspberry Pi Pico 2 and Pico 2 W
- Getting Started with Raspberry Pi Pico (and Pico W)
Where to Buy Raspberry Pi Pico 2?
The Raspberry Pi Pico 2 W board is widely available on many different stores. Check the following link to compare its price on different stores:
Subjects Covered in this Guide
In this guide, we’ll take a look at basic file and directory operations using the os MicroPython module:
- File operations:
- Create/open a file
- Rename a file
- Delete a file
- Writing to a file
- Appending to a file
- Reading from a file
- Directory operations:
- Create a new directory
- Listing files in a directory
- Remove a directory
The os MicroPython Module
In MicroPython, there’s a module called os that includes a wide range of functions to manage file operations. Additionally, it also comes with methods for tasks, like managing directories, accessing environment variables, and executing system commands.
The os module comes included by default on MicroPython firmware. More details about the os module can be found in the official documentation.
File Operations
Open a new file in Thonny IDE. We’ll show you some small snippets of code to illustrate how to perform file operations. Run the snippets as we show them to understand how everything works and see the returned results.
Creating a File
Creating a file in MicroPython is as easy as using the open() function and passing as argument the filename and the file opening mode. The open() function will open the file if it already exists, or it will create a new file if it doesn’t.
The different modes for opening a file are ‘w’, ‘r’, and ‘a’:
- ‘w’: writing mode — allows writing to a file, overwriting existing content;
- ‘r’: reading mode — enables reading from an existing file;
- ‘a’: appending mode — appending new data to the end of an existing file.
To create a new file, we can use the writing mode (‘w’). The following line of code will create a new file on the Raspberry Pi Pico filesystem using the open() method. The file is called testFile.txt, but you can rename it to whatever you want.
file = open('testFile.txt', 'w')
The first argument of the open() function is the file path. Because we’re creating the files on the root directory of the Raspberry Pi Pico filesystem, we can simply pass the file name.
Run this previous line on your Raspberry Pi by clicking on the Thonny green run icon.

A new empty file called testFile.txt was created on the Raspberry Pi filesystem.
To check if the file was created and saved, you can go to File > Save as… > Raspberry Pi Pico. You can see all the files saved on the Pico filesystem, including the testFile.txt we just created.

Another way to check the files on the Raspberry Pi Pico filesystem in Thonny IDE is by going to View > Files.
On the left sidebar, it will show you the files on your computer and the files on the Raspberry Pi Pico. You can click on the three-dash icon to refresh the filesystem.

The testFile.txt will be there, and you can double-click on it to open it.
Listing Files in the Filesystem
Instead of having to do this procedure to check the content of the filesystem, we can use a function from the os module that lists all files in the directory. For that, we need to import the os module first.
import os
And then, we can use the following command to check the contents of the filesystem:
os.listdir()
To test this, create a new file on Thonny IDE and copy the following two lines. We use the print() function to print the results on the Shell.
import os
print(os.listdir())
Run this section of code. It should print the current contents of the filesystem into the Shell. Notice that it should list the file you created previously, the testFile.txt.

Renaming a File
To rename a file, you can use the rename() method from the os module. Pass as arguments the current name of the file and the new name for the file. For example:
os.rename('old_file_name.txt', 'new_file_name.txt')
To change the name of our previous file, you can run the following example (it already checks the contents of the filesystem with os.listdir()).
import os
os.rename('testFile.txt','newNameFile.txt')
print(os.listdir())
Run these previous lines on your Raspberry Pi Pico. Notice that the name of the file was changed to newNameFile.txt.

Writing to a File
Writing to a file is very straightforward. You just need to use the write() method on the file object and pass as argument the data you want to write (it must be a string).
First, you need to open the file in writing mode.
file = open('newNameFile.txt', 'w')
For example, to write some data to the file:
file.write("Writing my first line")
After writing to the file, you should call the close() method to close the file and save its content.
file.close()
The write() method will overwrite any previous data on the file.
Appending to a File
If you want to add new data at the end of an existing file, you must write to the file in appending ‘a’ mode.
Open the file in appending mode:
file = open('newNameFile.txt', 'a')
Write to the file using the write() method:
file.write('This line was appended')
After writing to the file, you should call the close() method to close the file and save its content.
file.close()
Reading a File
To read a file, first you need to open it in reading ‘r’ mode. For example:
file = open('newNameFile.txt', 'r')
Then, we can read its content using the read() method on the file object. For example:
file.read()
We can save the content of the file in a variable, and then print it on the Shell:
content = file.read()
print("File content:")
print(content)
Don’t forget, to always close the file after performing file operations:
file.close()
The following code performs all the file operations we’ve seen previously:
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-files-directories-micropython/
import os
# Create a new file called testFile.txt
file = open('testFile.txt', 'w')
# List all files in the filesystem
print(os.listdir())
# Write content to the file
file.write("Writing my first line \n")
file.close()
# Rename the file
os.rename('testFile.txt','newNameFile.txt')
# List all files in the filesystem
print(os.listdir())
# Open the file in appending mode
file = open('newNameFile.txt', 'a')
# Append data to the file
file.write('This line was appended')
file.close()
# Open file in reading mode
file = open('newNameFile.txt', 'r')
# Read the content of the file
content = file.read()
# Print the content of the file
print("File content:")
print(content)
file.close()
If you run this previous code on your Raspberry Pi Pico, you should get something similar as shown in the screenshot below.

The Newline Character
In the previous code, notice that we use a \n at the end of this line:
file.write("Writing my first line \n")
In Python/MicroPython, the \n represents a newline character. It is used to represent the end of a line in a text file. It is a control character that signifies the beginning of a new line of text.
When working with files, the newline character is useful for formatting text. When writing multiple lines to a file, you use \n to separate each line.
Deleting a File
To delete a file, you can use os.remove() and pass as argument the file path. For example, to remove the newNameFile.txt, you can simply run the following:
import os
os.remove('newNameFile.txt')
To check if the file was actually removed, you can list all files in the directory, after the previous commands.
# List all files in the filesystem
print(os.listdir())
After running these previous lines, you can see that the newNameFile.txt is not on the filesystem anymore.
The ‘with’ Keyword for File Operations
The with keyword is used with file operations to ensure proper resource management and simplify code. Specifically, it’s often used with the open() function when dealing with files. For example:
with open('file_path', 'w') as file:
file.write('hello world!')
This syntax automatically handles opening and closing the file, ensuring that the file is properly closed when you’re done with it, even if an exception occurs during file operations.
The previous snippet is equivalent to the following:
file = open('file_path', 'w')
try:
file.write('hello world!')
finally:
file.close()
So, using the with keyword simplifies your code and ensures proper file management.
Directory Operations
The os module also comes with useful methods to work with directories (folders). Working with directories is very useful for organizing files. Especially if you’re working with multiple files.
Creating a Directory
Creating a new directory is as easy as using the mkdir() method from the os module. You should pass as argument the directory path. If you want to create the folder at the root of the Raspberry Pi Pico filesystem, you can just pass the directory name.
For example, the following code creates a new directory called my_directory at the root of the Raspberry Pi Pico filesystem:
import os
# Specify the directory path
dir_path = 'my_directory'
# Create a new directory
os.mkdir(dir_path)
Run this previous code to create the folder on the Pico filesystem.
You can check that new directory shows up on the list of files on the filesystem.

Listing Files in a Directory
As we’ve seen previously, you can use the listdir() method to list all files in the filesystem. If you want to list the files inside a particular folder, you just need to pass the folder path as an argument. For example, the following code will check the content of your new directory my_directory:
import os
dir_path = 'my_directory'
# List the files in the directory
files = os.listdir(dir_path)
print("Files in the directory:", files)
Run this previous code. In this case, it will return an empty array, because the directory is empty.

Deleting a Directory
The os module comes with a method to delete empty folders. You can use the rmdir() method and pass as argument the directory path. For example:
import os
directory_path = 'my_directory'
os.rmdir(directory_path)
If you want to delete a directory, first you need to delete its files before applying the rmdir() method.
Wrapping Up
In this tutorial, we covered how to handle files and folders on the Raspberry Pi Pico filesystem. You learned how to create, write, read, and delete files, and create, delete, and list directories.
If you want to learn how to do something similar, but using a microSD card, you can check out the following tutorial:
We hope this guide was useful.
Learn more about programming the Raspberry Pi Pico using MicroPython using our eBook:



