21
loading...
This website collects cookies to deliver better user experience
This is Day 5 of the #100DaysOfPython challenge.
glob
library to reading a base directory that we have set with files and put them into a list.icons
directory.hello-read-dir-python
directory and add an icons directory to read from.# 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
icons
directory. They do not necessarily need to be imaged, but the code written will target that folder.# Startup the notebook server
$ pipenv run jupyter-lab
# ... Server is now running on http://localhost:8888/lab
hello-read-dir-python/docs/<your-file-name>
.glob
library allows us to recurise through a directory and return a list of all the files that match the glob pattern.icons
directory from the script..hello-read-dir-python/docs/<your-file-name>
to have the following:import glob
from os.path import join, dirname, abspath
glob
library imported, then we can use it to read the directory and do the heavy lifting for us.# 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}/*"))
['/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']
glob
library to programmatically get all the matching files within a folder.