{"id":183167,"date":"2025-12-26T13:55:20","date_gmt":"2025-12-26T13:55:20","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=183167"},"modified":"2025-12-26T15:07:15","modified_gmt":"2025-12-26T15:07:15","slug":"raspberry-pi-pico-files-directories-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-files-directories-micropython\/","title":{"rendered":"Raspberry Pi Pico: Handling Files and Directories (MicroPython)"},"content":{"rendered":"\n<p>Learn how to handle files and directories with the Raspberry Pi Pico programmed with MicroPython. We&#8217;ll cover how to create and perform operations with files and directories saved permanently on the Raspberry Pi Pico filesystem.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-Files-folders.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Handling Files and Directories MicroPython\" class=\"wp-image-183205\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-Files-folders.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-Files-folders.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-Files-folders.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-Files-folders.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-Files-folders.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p>Learning this subject is useful to create files to save configurations, save variables\u2019 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites \u2013 MicroPython Firmware<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\">Programming Raspberry Pi Pico using MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<p>If you\u2019re still getting started with the Raspberry Pi Pico, follow one of these getting-started guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-2-w\/\">Getting Started with Raspberry Pi Pico 2 and Pico 2 W<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/\">Getting Started with Raspberry Pi Pico (and Pico W)<\/a><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Where to Buy Raspberry Pi Pico 2?<\/h4>\n\n\n\n<p>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:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-2-w\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Buy Raspberry Pi Pico 2 W<\/strong><\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Subjects Covered in this Guide<\/h2>\n\n\n\n<p>In this guide, we\u2019ll take a look at basic file and directory operations using the <span class=\"rnthl rntliteral\">os<\/span> MicroPython module:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>File operations:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Create\/open a file<\/li>\n\n\n\n<li>Rename a file<\/li>\n\n\n\n<li>Delete a file<\/li>\n\n\n\n<li>Writing to a file<\/li>\n\n\n\n<li>Appending to a file<\/li>\n\n\n\n<li>Reading from a file<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Directory operations:<\/strong><ul><li>Create a new directory<\/li><\/ul>\n<ul class=\"wp-block-list\">\n<li>Listing files in a directory<\/li>\n\n\n\n<li>Remove a directory<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">The <em>os <\/em>MicroPython Module<\/h2>\n\n\n\n<p>In MicroPython, there\u2019s a module called <span class=\"rnthl rntliteral\">os<\/span> 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. <\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">os<\/span> module comes included by default on MicroPython firmware. More details about the os module can be found in the <a href=\"https:\/\/docs.micropython.org\/en\/latest\/library\/os.html\" target=\"_blank\" rel=\"noopener\" title=\"\">official documentation<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">File Operations<\/h2>\n\n\n\n<p>Open a new file in Thonny IDE. We\u2019ll 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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a File<\/h3>\n\n\n\n<p>Creating a file in MicroPython is as easy as using the <span class=\"rnthl rntliteral\">open()<\/span> function and passing as argument the filename and the file opening mode. The <span class=\"rnthl rntliteral\">open()<\/span> function will open the file if it already exists, or it will create a new file if it doesn\u2019t.<\/p>\n\n\n\n<p>The different modes for opening a file are <span class=\"rnthl rntliteral\">&#8216;w&#8217;<\/span>, <span class=\"rnthl rntliteral\">&#8216;r&#8217;<\/span>, and <span class=\"rnthl rntliteral\">&#8216;a&#8217;<\/span>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">&#8216;w&#8217;<\/span>: writing mode \u2014 allows writing to a file, overwriting existing content;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">&#8216;r&#8217;<\/span>: reading mode \u2014 enables reading from an existing file;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">&#8216;a&#8217;<\/span>: appending mode \u2014 appending new data to the end of an existing file.<\/li>\n<\/ul>\n\n\n\n<p>To create a new file, we can use the writing mode (<span class=\"rnthl rntliteral\">&#8216;w&#8217;<\/span>). The following line of code will create a new file on the Raspberry Pi Pico filesystem using the <span class=\"rnthl rntliteral\">open()<\/span> method. The file is called <span class=\"rnthl rntliteral\">testFile.txt<\/span>, but you can rename it to whatever you want.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file = open('testFile.txt', 'w')<\/code><\/pre>\n\n\n\n<p>The first argument of the <span class=\"rnthl rntliteral\">open()<\/span> function is the file path. Because we\u2019re creating the files on the root directory of the Raspberry Pi Pico filesystem, we can simply pass the file name.<\/p>\n\n\n\n<p>Run this previous line on your Raspberry Pi by clicking on the Thonny green run icon.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"634\" height=\"206\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Create-File-Thonny-IDE.png?resize=634%2C206&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico with Thonny IDE - Creating a new file\" class=\"wp-image-183170\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Create-File-Thonny-IDE.png?w=634&amp;quality=100&amp;strip=all&amp;ssl=1 634w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Create-File-Thonny-IDE.png?resize=300%2C97&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 634px) 100vw, 634px\" \/><\/figure><\/div>\n\n\n<p>A new empty file called <span class=\"rnthl rntliteral\">testFile.txt<\/span> was created on the Raspberry Pi filesystem.<\/p>\n\n\n\n<p>To check if the file was created and saved, you can go to <strong>File<\/strong> &gt; <strong>Save as\u2026<\/strong> &gt; <strong>Raspberry Pi Pico<\/strong>. You can see all the files saved on the Pico filesystem, including the <span class=\"rnthl rntliteral\">testFile.txt<\/span> we just created.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"545\" height=\"327\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Test-File-saved-on-RPi-Pico-Filesystem.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Test file created and saved on the RPi Pico filesystem\" class=\"wp-image-183171\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Test-File-saved-on-RPi-Pico-Filesystem.png?w=545&amp;quality=100&amp;strip=all&amp;ssl=1 545w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Test-File-saved-on-RPi-Pico-Filesystem.png?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<p>Another way to check the files on the Raspberry Pi Pico filesystem in Thonny IDE is by going to <strong>View<\/strong> &gt; <strong>Files<\/strong>.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"579\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Files-saved-RPi-Filesystem-Sidebar-Thonny-IDE.png?resize=750%2C579&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"File saved on the RPi Pico filesystem shown in Thonny IDE left sidebar\" class=\"wp-image-183172\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Files-saved-RPi-Filesystem-Sidebar-Thonny-IDE.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Files-saved-RPi-Filesystem-Sidebar-Thonny-IDE.png?resize=300%2C232&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The <span class=\"rnthl rntliteral\">testFile.txt<\/span> will be there, and you can double-click on it to open it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Listing Files in the Filesystem<\/h3>\n\n\n\n<p>Instead of having to do this procedure to check the content of the filesystem, we can use a function from the <span class=\"rnthl rntliteral\">os<\/span> module that lists all files in the directory. For that, we need to import the <span class=\"rnthl rntliteral\">os<\/span> module first.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import os<\/code><\/pre>\n\n\n\n<p>And then, we can use the following command to check the contents of the filesystem:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>os.listdir()<\/code><\/pre>\n\n\n\n<p>To test this, create a new file on Thonny IDE and copy the following two lines. We use the <span class=\"rnthl rntliteral\">print()<\/span> function to print the results on the Shell.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import os\nprint(os.listdir())<\/code><\/pre>\n\n\n\n<p>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 <span class=\"rnthl rntliteral\">testFile.txt<\/span>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"612\" height=\"519\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-list-files.png?resize=612%2C519&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico on Thonny IDE - list files\" class=\"wp-image-183173\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-list-files.png?w=612&amp;quality=100&amp;strip=all&amp;ssl=1 612w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-list-files.png?resize=300%2C254&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 612px) 100vw, 612px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Renaming a File<\/h3>\n\n\n\n<p>To rename a file, you can use the <span class=\"rnthl rntliteral\">rename()<\/span> method from the <span class=\"rnthl rntliteral\">os<\/span> module. Pass as arguments the current name of the file and the new name for the file. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>os.rename('old_file_name.txt', 'new_file_name.txt')<\/code><\/pre>\n\n\n\n<p>To change the name of our previous file, you can run the following example (it already checks the contents of the filesystem with <span class=\"rnthl rntliteral\">os.listdir()<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import os\nos.rename('testFile.txt','newNameFile.txt')\nprint(os.listdir())<\/code><\/pre>\n\n\n\n<p>Run these previous lines on your Raspberry Pi Pico. Notice that the name of the file was changed to <span class=\"rnthl rntliteral\">newNameFile.txt<\/span>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"634\" height=\"519\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-Rename-File.png?resize=634%2C519&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico on Thonny IDE . Rename a File\" class=\"wp-image-183174\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-Rename-File.png?w=634&amp;quality=100&amp;strip=all&amp;ssl=1 634w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-Rename-File.png?resize=300%2C246&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 634px) 100vw, 634px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Writing to a File<\/h3>\n\n\n\n<p>Writing to a file is very straightforward. You just need to use the <span class=\"rnthl rntliteral\">write()<\/span> method on the <span class=\"rnthl rntliteral\">file<\/span> object and pass as argument the data you want to write (it must be a string). <\/p>\n\n\n\n<p>First, you need to open the file in writing mode.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file = open('newNameFile.txt', 'w')<\/code><\/pre>\n\n\n\n<p>For example, to write some data to the file:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file.write(\"Writing my first line\")<\/code><\/pre>\n\n\n\n<p>After writing to the file, you should call the <span class=\"rnthl rntliteral\">close()<\/span> method to close the file and save its content.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file.close()<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">write()<\/span> method will overwrite any previous data on the file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Appending to a File<\/h3>\n\n\n\n<p>If you want to add new data at the end of an existing file, you must write to the file in appending <span class=\"rnthl rntliteral\">&#8216;a&#8217;<\/span> mode.<\/p>\n\n\n\n<p>Open the file in appending mode:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file = open('newNameFile.txt', 'a')<\/code><\/pre>\n\n\n\n<p>Write to the file using the <span class=\"rnthl rntliteral\">write()<\/span> method:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file.write('This line was appended')<\/code><\/pre>\n\n\n\n<p>After writing to the file, you should call the <span class=\"rnthl rntliteral\">close()<\/span> method to close the file and save its content.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file.close()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reading a File<\/h3>\n\n\n\n<p>To read a file, first you need to open it in reading <span class=\"rnthl rntliteral\">&#8216;r&#8217;<\/span> mode. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file = open('newNameFile.txt', 'r')<\/code><\/pre>\n\n\n\n<p>Then, we can read its content using the <span class=\"rnthl rntliteral\">read()<\/span> method on the <span class=\"rnthl rntliteral\">file<\/span> object. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file.read()<\/code><\/pre>\n\n\n\n<p>We can save the content of the file in a variable, and then print it on the Shell:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>content = file.read()\nprint(\"File content:\")\nprint(content)<\/code><\/pre>\n\n\n\n<p>Don\u2019t forget, to always close the file after performing file operations:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file.close()<\/code><\/pre>\n\n\n\n<p>The following code performs all the file operations we\u2019ve seen previously:<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-files-directories-micropython\/\n\nimport os\n\n# Create a new file called testFile.txt\nfile = open('testFile.txt', 'w')\n\n# List all files in the filesystem\nprint(os.listdir())\n\n# Write content to the file\nfile.write(&quot;Writing my first line \\n&quot;)\nfile.close()\n\n# Rename the file\nos.rename('testFile.txt','newNameFile.txt')\n\n# List all files in the filesystem\nprint(os.listdir())\n\n# Open the file in appending mode\nfile = open('newNameFile.txt', 'a')\n# Append data to the file\nfile.write('This line was appended')\nfile.close()\n\n# Open file in reading mode\nfile = open('newNameFile.txt', 'r')\n\n# Read the content of the file\ncontent = file.read()\n\n# Print the content of the file\nprint(&quot;File content:&quot;)\nprint(content)\nfile.close()\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/File_Operations.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>If you run this previous code on your Raspberry Pi Pico, you should get something similar as shown in the screenshot below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"637\" height=\"239\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-File-Operations.png?resize=637%2C239&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico Thonny IDE - file operations - results on Thonny IDE\" class=\"wp-image-183175\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-File-Operations.png?w=637&amp;quality=100&amp;strip=all&amp;ssl=1 637w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-File-Operations.png?resize=300%2C113&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 637px) 100vw, 637px\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\">The Newline Character<\/h4>\n\n\n\n<p>In the previous code, notice that we use a <strong>\\n<\/strong> at the end of this line:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file.write(\"Writing my first line <strong>\\n<\/strong>\")<\/code><\/pre>\n\n\n\n<p>In Python\/MicroPython, the <span class=\"rnthl rntliteral\">\\n<\/span> 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.&nbsp;<\/p>\n\n\n\n<p>When working with files, the newline character is useful for formatting text. When writing multiple lines to a file, you use <span class=\"rnthl rntliteral\">\\n<\/span> to separate each line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Deleting a File<\/h3>\n\n\n\n<p>To delete a file, you can use <span class=\"rnthl rntliteral\">os.remove()<\/span> and pass as argument the file path. For example, to remove the <span class=\"rnthl rntliteral\">newNameFile.txt<\/span>, you can simply run the following:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import os\nos.remove('newNameFile.txt')<\/code><\/pre>\n\n\n\n<p>To check if the file was actually removed, you can list all files in the directory, after the previous commands.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># List all files in the filesystem\nprint(os.listdir())<\/code><\/pre>\n\n\n\n<p>After running these previous lines, you can see that the <span class=\"rnthl rntliteral\">newNameFile.txt<\/span> is not on the filesystem anymore.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The \u2018<em>with\u2019<\/em> Keyword for File Operations<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\"><strong>with<\/strong><\/span> keyword is used with file operations to ensure proper resource management and simplify code. Specifically, it&#8217;s often used with the <span class=\"rnthl rntliteral\">open()<\/span> function when dealing with files. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>with open('file_path', 'w') as file:\n&nbsp; &nbsp; file.write('hello world!')<\/code><\/pre>\n\n\n\n<p>This syntax automatically handles opening and closing the file, ensuring that the file is properly closed when you&#8217;re done with it, even if an exception occurs during file operations.<\/p>\n\n\n\n<p>The previous snippet is equivalent to the following:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>file = open('file_path', 'w')\ntry:\n&nbsp; &nbsp; file.write('hello world!')\nfinally:\n&nbsp; &nbsp; file.close()<\/code><\/pre>\n\n\n\n<p>So, using the <span class=\"rnthl rntliteral\">with<\/span> keyword simplifies your code and ensures proper file management.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Directory Operations<\/h2>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">os<\/span> module also comes with useful methods to work with directories (folders). Working with directories is very useful for organizing files. Especially if you\u2019re working with multiple files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a Directory<\/h3>\n\n\n\n<p>Creating a new directory is as easy as using the <span class=\"rnthl rntliteral\">mkdir()<\/span> method from the <span class=\"rnthl rntliteral\">os<\/span> 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.<\/p>\n\n\n\n<p>For example, the following code creates a new directory called <span class=\"rnthl rntliteral\"><em>my_directory<\/em><\/span> at the root of the Raspberry Pi Pico filesystem:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import os\n\n# Specify the directory path\ndir_path = 'my_directory'\n# Create a new directory\nos.mkdir(dir_path)<\/code><\/pre>\n\n\n\n<p>Run this previous code to create the folder on the Pico filesystem.<\/p>\n\n\n\n<p>You can check that new directory shows up on the list of files on the filesystem.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"718\" height=\"547\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-Create-New-Directory.png?resize=718%2C547&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico on Thonny IDE - Create new directory\" class=\"wp-image-183179\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-Create-New-Directory.png?w=718&amp;quality=100&amp;strip=all&amp;ssl=1 718w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Thonny-IDE-Create-New-Directory.png?resize=300%2C229&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 718px) 100vw, 718px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Listing Files in a Directory<\/h3>\n\n\n\n<p>As we\u2019ve seen previously, you can use the <span class=\"rnthl rntliteral\">listdir()<\/span> 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 <span class=\"rnthl rntliteral\"><em>my_directory<\/em><\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import os\n\ndir_path = 'my_directory'\n\n# List the files in the directory\nfiles = os.listdir(dir_path)\nprint(\"Files in the directory:\", files)<\/code><\/pre>\n\n\n\n<p>Run this previous code. In this case, it will return an empty array, because the directory is empty.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"250\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-CheckFiles-in-Directory-Thonny-IDE.jpg?resize=624%2C250&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico on Thonny IDE - checking files inside a directory\" class=\"wp-image-183180\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-CheckFiles-in-Directory-Thonny-IDE.jpg?w=624&amp;quality=100&amp;strip=all&amp;ssl=1 624w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-CheckFiles-in-Directory-Thonny-IDE.jpg?resize=300%2C120&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Deleting a Directory<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">os<\/span> module comes with a method to delete empty folders. You can use the <span class=\"rnthl rntliteral\">rmdir()<\/span> method and pass as argument the directory path. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import os\ndirectory_path = 'my_directory'\nos.rmdir(directory_path)<\/code><\/pre>\n\n\n\n<p>If you want to delete a directory, first you need to delete its files before applying the <span class=\"rnthl rntliteral\">rmdir()<\/span> method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>If you want to learn how to do something similar, but using a microSD card, you can check out the following tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-microsd-card-micropython\/\">Raspberry Pi Pico: MicroSD Card Guide with Datalogging Example (MicroPython)<\/a><\/li>\n<\/ul>\n\n\n\n<p>We hope this guide was useful.<\/p>\n\n\n\n<p>Learn more about programming the Raspberry Pi Pico using MicroPython using our eBook:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico\/Pico W with MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to handle files and directories with the Raspberry Pi Pico programmed with MicroPython. We&#8217;ll cover how to create and perform operations with files and directories saved permanently on &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: Handling Files and Directories (MicroPython)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-files-directories-micropython\/#more-183167\" aria-label=\"Read more about Raspberry Pi Pico: Handling Files and Directories (MicroPython)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":183205,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324,326],"tags":[],"class_list":["post-183167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-micropython"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-Files-folders.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/183167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=183167"}],"version-history":[{"count":11,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/183167\/revisions"}],"predecessor-version":[{"id":186523,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/183167\/revisions\/186523"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/183205"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=183167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=183167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=183167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}