Generating A List of Dictionaries With Python

Jim Flermoen
DevOps.dev
Published in
4 min readMay 17, 2023

--

Overview

It seems like it’s been a while since I have written an article here, but it’s only been a few weeks and I am back. I have been learning Python for the last couple of weeks. Python is one of the most popular coding languages and has been around since 1991. One of the many reasons it is liked so much is how easy it is to read. It is used by some big companies that you have heard of and probably used like Instagram, Google, Netflix, and more.

The Project

For this project, there is a company that wants a script that can be used to learn about different files located on various machines. My task for this project is to create a script that generates a list of dictionaries about files in my working directory. Then it needs to print the list. The way I did this was to break down what each command should be. Then I put them in order so they would give me the output I was looking for. So let's get back to work.

Import The Operating System

With Python, we can import different modules with the import function. Whether it is from one of the files on your machine, a library, or a package that with a Python SDK. First and foremost, we need to import the operating system of my local machine to my IDE. The IDE I am using is AWS Cloud 9. This will make it possible to list out my working directory. The command is import os as seen below.

import os

Create An Empty list

Next, I created an empty list and named it files. With the list created, we will be able to put the information about the files we pull from the machines. It is a very simple command.

files = []

Working Directory Variable

Here we need to make a variable for our working directory. A variable is a name we give to our working directory that is easier for us to read and understand, but as Python goes, it stores the data for our working directory. I am calling the variable name for the working directory “working_dir”. To pull the data we need to use the get current working directory from my operating system. the command to do this is below.

working_dir = os.getcwd()

Pulling Data

Now that we have all the data stored in the variable “working_dir”, we will make it into a list. We already created our list and named it “files”. With this next line of code, we are telling Python to Iterate through the data and that we will be putting it in the list we created called file. I know it’s confusing but stick with me. the next code is:

for file in os.listdir(working_dir):

File Path and Size

The next two lines of code will pull the information requested. Which is the path of the file(location) and the size of the file and stores the variable “file_path” and “file_size”.

 file_path = os.path.realpath(file)
file_size = os.path.getsize(file)

A List Of Dictionaries

With This line of code, we turn our list into a list of dictionaries with the information we want. A dictionary has a key and a value. The key is like a name or category and the value is the information in that category. So for the “file_path,” I named the key “path” and the value is the actual path of the file. I know, it’s confusing but you will see the result soon. For the “file_size,” I named the key “size” and the value is the actual size of the file. The append in the code is the command needed to add the data as a dictionary to the file. The code is below.

    files.append({'path': file_path, 'size': file_size})

This is what the script looks like without the inline commenting.

This is what it looks like when I broke it down with the inline comments

Output

Here is the final result of the list of dictionaries. Now you can see Key: Value as: ‘path’: ‘/home/ec2-user/environment/Green_Money_team/.git’, and Key: Value ‘size’:198

Thank you for finishing with me again. Until the next one.

--

--

I am a Veteran and DevOps Engineer. I enjoy the outdoors with my family; fishing, hunting, camping, hiking. etc.