Managing files on the Pico with MicroPython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey robot makers i'm going to show you how to manage files on your raspberry pi pico and go through some of the differences between circuit python and regular micropython as well so if i go over to thony which is just running on a raspberry pi here and funny works exactly the same whether it's on a mac whether it's on windows or raspberry pi and we can see here i've got a raspberry pi pico connected to the shell so i've clicked down here to select micro python running raspberry pi pico i'll have a look at circuit python in a minute we create a file hello world let's just do print hello world and i'm going to save that clicking on here and it says do you want to save to the computer or to the raspberry pi pico so i'm going to select raspberry pi pico and then i'm going to give it the name hello dot pi so that file has now been saved to the raspberry pi pico but what other files are on there so the one way that we can find that from within thon is if we click on the view and then files option we can then see the the files are on my computer as well as the files that are on the raspberry pi pico so we can see there we've got hello.pi and there's also another folder there that's called test how do i do things like rename that file so currently there is no way to do this from within funny if i right click that file i can't change the name of that file i can look at the properties i can create new folders i can delete folders but i can't rename files so within micropython there is a module that's called os if i do import os and then i do dir os to have a look what what the modules within it you can see there there's remove there is change directory get current working directory list directory make directory rename remove directory stats and some other ones there but let's have a look at one of these if i do os dot rename and then i put in the name of the file which is hello.pi and then do comma and then the name that we want to give it so hello underscore world dot pi then it's actually renamed that if i go to this little option here and do refresh you can now see that it says hello underscore world.pi so we can actually use the python module os to do lots of file management for us so we can do things like make dir and let's call this test2 and if we now do os dot list dir it will show us all the different files and folders that are on our raspberry pi pco and if i just hit that refresh there you'll also see that test2 has now appeared we want to actually go inside one of the folders we have to change the current working directory so os dot change working directory and then we pass it the name of the directory that we want to change into in this case test2 so if i now do os dot get current working directory it will show you that we're now in slash test two which is inside that folder there so anything that we now create in there will be created within that folder and similarly if we do os dot list dir it will do that listing from within that folder so if we want to get back to the root we do os dot change directory and then we pass in the root which is just that forward slash so now do a list directory again you'll see that we can see those three files and folders again and we can also do os dot get current working directory to show that we're in the root to delete files it's just the the remove command so we do os dot remove and we can say remove hello underscore world dot pi and that will remove that file if you now do os.list dir you'll see that that file no longer exists and if i refresh that folder there you can see that's disappeared if you want to remove a folder you can just use the same command you can do os.remove and then type the name of the folder to be removed such as test so if i now refresh that you'll see that test disappears and i can do that again to remove that folder so if i now refresh that you can see that that's disappeared so let's have a look at some of the commands in a bit more detail so we've got os.list dir which lists all the files in the current working directory it doesn't differentiate between folders and files you have to just know that unfortunately there are some properties that we can look at we look at os.rename that renames the file or folder os.remove deletes the file os.stat gives us some stats about how large the file is for example os.change directory changes the current working directory whereas os.get current working directory returns that current working directory and then there's two there for making directories folders as we know them or removing directories and it seems to be that removing a file will automatically use the remove directory command if you if you use that you can use them interchangeably one of the reasons that we need to do this in the command line or using the thunder is because the raspberry pi p code it doesn't appear as a drive when we plug it into the computer so if i got this raspberry pi peak or plugged in it will appear as a drive if i hold down the boot select button and insert it we'll see that it will pop up as soon as i let go of the button as rpi rp2 however we don't see the files and folders that are actually stored on here the python files it's kind of a special folder that it puts in place and it's just there for flashing firmware so i've got a raspberry pi p code that's been flashed with circuit python so when i plug this in and i'm not holding down the boot select button it will actually show up as a drive on the file system so you can see their circuit pi unlike the standard version of micro python we can actually see what's on this device so let's just click on it there and see that we've got code.pi so if i uh copy that paste that in there as well let's call it code2.pi and if we now go back to thony and we connect this as a circuit python device we can now see code and code 2 and this works just like a regular drive they've made this a little bit easier to use a sacrifice of some space i believe so hopefully you found this one interesting um for more videos about raspberry pi pecos or robotics in general why not check out the rest of the videos on my channel thanks for watching and i shall see you soon
Info
Channel: Kevin McAleer
Views: 13,199
Rating: undefined out of 5
Keywords: Managing files on the Raspberry Pi Pico with MicroPython, Raspberry Pi Pico, Pico, Raspberry Pi, MicroPython, circuitpython raspberry pi, circuitpython vs micropython, file management, python, python os library, add files, remove files, rename files, create folder, delete folder, os.listdir, list directory, Kevin McAleer, Managing files on teh raspberry pi pico with micropython, raspberry pi pico micropython, micropython tutorial, micropython vs circuitpython
Id: g599FlcKgxA
Channel Id: undefined
Length: 6min 4sec (364 seconds)
Published: Tue May 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.