CLI tools example: show all tools

fronkan profile image Fredrik Bengtsson ・2 min read

I wanted to have a simple tool as my first example in the series. So this first tool will just print all the tools we have stored in our bin folder. Especially useful when you forget what you have named your other tools. I will assume you have read the previous parts of the series on how everything is set up.

I have named this script tools.py and it is just 8 lines of code. It looks like this:

from pathlib import Path
bin_folder = Path(__file__).parent

for tool in bin_folder.iterdir():
    name = tool.name
    if name.startswith("_"):

So this uses the __file__ attribute to access the path to the tools.py script, which of course is stored in the bin folder. Therefore, the parent of the script is the bin folder. Here I am using Path-object which I prefer over the corresponding functions in the os.path-module. Check out this part of the Python documentation for mapping between os.path and pathlib.Path.

Moving on, we iterate over the content of the directory and print the name of each tool. The Path.name attribute is just the name of the file. I also skip all tools which have a name that has a leading underscore. This is just a personal design decision, as I have used this naming convention for files that are for internal use only.

I hope you liked this simple CLI-tool. I am working on a post with one of the tools I have been using the most, which helps with finding and activating virtual environments. So if that sounds interesting, stay tuned for that one.

Posted on by:

fronkan profile

Fredrik Bengtsson


Software engineering consultant at Alten. Interested in data science, machine learning, and automation. I also have a crush on the python language.


markdown guide