Reading a file directory with Python
Tags: beginners, python, 100daysofpython
Published: 2021-08-24 10:15:11

_**I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to [visit my blog](https://blog.dennisokeeffe.com) and subscribe to my newsletter. [Follow me on Twitter](https://twitter.com/dennisokeeffe92).**_

> 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.

## Prerequisites

1. Familiarity with [Pipenv](https://pipenv.pypa.io/en/latest/). See [here](https://blog.dennisokeeffe.com/blog/2020-07-04-abcs-of-pipenv) for my post on Pipenv.
2. Familiarity with [JupyterLab](https://jupyterlab.readthedocs.io/en/stable/gettingstarted/install.html). See [here](https://blog.dennisokeeffe.com/blog/2021-07-19-hello-jupyter-notebook) for my post on JupyterLab.
3. A list of icons (or just any files) in an `icons` directory.

## Getting started

Let's create the `hello-read-dir-python` directory and add an icons directory to read from.

```s
# Make the `hello-read-dir-python` directory
$ mkdir hello-read-dir-python
$ cd hello-read-dir-python
# Create a folder to place your icons
$ mkdir icons

# Init the virtual environment
$ pipenv --three
$ pipenv install pillow
$ pipenv install --dev jupyterlab
```

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.

```s
# Startup the notebook server
$ pipenv run jupyter-lab
# ... Server is now running on http://localhost:8888/lab
```

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 `hello-read-dir-python/docs/<your-file-name>`.

We will create two cells to handle two parts of this script:

1. A cell to import the require packages.
2. A cell to read the directory.

## Importing the packages

We will use the [glob](https://docs.python.org/3/library/glob.html) library to read the directory.

The `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](https://docs.python.org/3/library/os.path.html?highlight=os%20path#module-os.path) library to create a path to our `icons` directory from the script..

Update `hello-read-dir-python/docs/<your-file-name>` to have the following:

```py
import glob
from os.path import join, dirname, abspath
```

## 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.

```py
# Assign icons_dir as a path to the icons directory from the script file
icons_dir = join(dirname(abspath("__file__")), '../icons')
print(glob.glob(f"{icons_dir}/*"))
```

In my case, the following is printed to the console for the four files that I have in that folder:

```s
['/path/to/hello-read-dir-python/docs/../icons/git.png', '/path/to/hello-read-dir-python/docs/../icons/python.png', '/path/to/hello-read-dir-python/docs/../icons/aws.png', '/path/to/hello-read-dir-python/docs/../icons/dok.png']
```

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.

## Summary

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. [The ABCs of Pipenv](https://blog.dennisokeeffe.com/blog/2020-07-04-abcs-of-pipenv) for the minimum you will need.
2. [Hello, JupyterLab](https://blog.dennisokeeffe.com/blog/2021-07-19-hello-jupyter-notebook).
3. [glob](https://docs.python.org/3/library/glob.html)
4. [os.path](https://docs.python.org/3/library/os.path.html?highlight=os%20path#module-os.path)


_Originally posted on my [blog](https://blog.dennisokeeffe.com/blog/2021-07-24-reading-a-file-directory-with-python). To see new posts without delay, read the posts there and subscribe to my newsletter._