Skip to main content

Making Life Straight With Loops - 1

Don't let anyone stop you from learning.

Programming is powerful. This blog post shows how basic programming constructs can be used to save you time in your day to day manual tasks.

Let's get started.

Use Case 1: That time when I had to search a specific string missing from multiple files

Searching for a string in a single file is easy. It is also easy to search for something in multiple files once you open all those files in your favorite text editor. But what if all you have is notepad? badly configured machines can leave you stranded with just a notepad and no scope for installing any relevant software.

For eg. consider the following YAML files.

In order to create your own YAML files go here and follow the instructions.

Above is the list of configuration files for your servers. But some of these files don't have "sha512WithECDSAEncryption" signature algorithm inside them. You need to find those configuration files.

How to reach those files using python?

I believe the first problem we face is how can we access all the files present in any folder inside my system using python.

To this, we use a simple module called 'os'.

Take a look at the following code. You can directly take the code and run it in your machines to see how it works. (The code might not be visible depending upon if github is accessible through your network or not)

Now that we're capable of traveling inside our machine using python, let's see how to solve our particular problem.

Use Case 2: That time when I had to create an excel to catalog data

Moving forward with the above example, let's say you have to keep a catalog of all the YAML files and note down if they support "sha512WithECDSAEncryption" signature algorithm or not.

One way of doing it is to hit rename, copy the name of the file, paste it in your excel, check if it supports the signature, save and repeat.

The other way, involves the following code:

In order for the above code to work, you need to install an external module - openpyxl. Use the following code in your command line/terminal to install the external module.

python -m pip install openpyxl

more on this library can be found here.

A good exercise to test your understanding would be to try to read the excel you just created.

I hope these two use cases were helpful to you. There's going to be another post where we'll cover 3 more use cases which I hope will help you even further.

Please reach out to me at in case of any doubts.