Day 3–101 Days of DevOps — A python script to search for a file

Prashant Lakhera
3 min readJul 3, 2021

--

Welcome to Day 3of 101 Days of DevOps. The topic for today is a python script to search for a file. As a DevOps/System Administrator, this is one of the common tasks we encounter as a part of our daily job. The idea behind it is to create a find utility using Python to pass directory names and files to search.

To view the complete course, please check the below url.

For more info, register via the below link

YouTube Channel link

Solution Video(A python script to search for a file)

On Day 1, we discussed OS Module. As part of the discussion, we discussed os.walk(). If you want to brush your concept, I am attaching the link; else, please check the above video if you directly want to go to the solution.

So far, our code looks like this, whereby using os.walk(), we are iterating over the /etc directory and then using os.path.join() we combine directory with the filename.

import os

for dirpath, dirname, filename in os.walk("/etc"):
for file in filename:
comp_path= os.path.join(dirpath,file)

To find out the specific file, we can use the if statement and then only print the complete path if the file matches.

if file == "hosts":
print(comp_path)

So our complete code will look like below.

import os

for dirpath, dirname, filename in os.walk("/etc"):
for file in filename:
comp_path= os.path.join(dirpath,file)
if file == "hosts":
print(comp_path)

If you save this code in find_files.py and search for hosts file in /etc directory, you will get the below output.

python3 find_files.py           
/etc/hosts

The second version of this script is to make it user-friendly so that it will accept arguments like the path to search(e.g.,/etc.) and then filename(hosts) on the command line; we will achieve that with the help of the argparser module.

These are the steps you need to follow to use argparse

  1. Import the Python argparse library
import argparse

2. Create the parser

my_parser = argparse.ArgumentParser(description='Reading the directory path to find the file')

3. Add optional and positional arguments to the parser. Here we are passing two-argument pathname to specify the path and filesearch for the file to search.

my_parser.add_argument("pathname",help='Please enter the directory path ')
my_parser.add_argument("filesearch",
help='Please enter the filename to search')

4. Execute .parse_args()

args = my_parser.parse_args()

If you execute your script with the -h(help) option, you will get the output below.

python3 find_files.py -h
usage: find_files.py [-h] pathname filesearch
Reading the directory path to find the filepositional arguments:
pathname Please enter the directory path
filesearch Please enter the filename to search
optional arguments:
-h, --help show this help message and exit

Now you need to make few changes to your code. Rather than passing /etc to os.walk(), you need to pass args.pathname, and similarly, rather than passing filename directly, i.e., hosts, you need to pass args.filesearch.

for dirpath, dirname, filename in os.walk(args.pathname):
for file in filename:
comp_path= os.path.join(dirpath,file)
if file == args.filesearch:
print(comp_path)

To execute your script.

python3 find_files.py /etc hosts
/etc/hosts

Assignment:

  1. Write a simple script to find a file greater than X days old. X can be any number, let say 7 days.

Hint: You need to use the datetime module.

I am looking forward to you guys joining the amazing journey.

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

No responses yet