Reading a file directory with Python
This is Day 5 of the #100DaysOfPython challenge.
This post will use the
glob library to reading a base directory that we have set with files and put them into a list.
Let's create the
hello-read-dir-python directory and add an icons directory to read from.
Ensure to put some files into the
icons directory. They do not necessarily need to be imaged, but the code written will target that folder.
Now we can start up the notebook server.
The server will now be up and running.
Creating the notebook
Once on http://localhost:8888/lab, select to create a new Python 3 notebook from the launcher.
Ensure that this notebook is saved in
We will create two cells to handle two parts of this script:
- A cell to import the require packages.
- A cell to read the directory.
Importing the packages
We will use the glob library to read the directory.
glob library allows us to recurise through a directory and return a list of all the files that match the glob pattern.
We also use the packages from the os.path library to create a path to our
icons directory from the script..
hello-read-dir-python/docs/<your-file-name> to have the following:
Reading the directory
Once we have the
glob library imported, then we can use it to read the directory and do the heavy lifting for us.
In my case, the following is printed to the console for the four files that I have in that folder:
Now with that list assigned to the variable, we can use that list to iterate through and do what we need to from those files e.g. manipulate the image, read the file contents, write back file contents and more.
Today's post demonstrated how to use the
glob library to programmatically get all the matching files within a folder.
I use techniques such as this within my own blog repository to grab relevant markdown and image files to generate the blog post images or do things with the blog metadata (like creating the list for my home page).
Resources and further reading
1,200+ PEOPLE ALREADY JOINED ❤️️
Get fresh posts + news direct to your inbox.
No spam. We only send you relevant content.