NICT Club Blogs.

Effortlessly Find Files and Folders with Python

Abik Maharjan
Abik Maharjan

Seaching for files or even folders manually is a tedious task. I could use an in-built search function, but even that doesnot search every nook and cranny. Have you ever been in a situation where you know the name of the file or the directory but you don't remember where you saved it. You start searching for it manually since it didnot show up in the in-built search feature. Well, I have been there too. It's like finding a needle in a haystack. You need to find a file/folder in gazillions of files you have and their structure is just ewwww. This dull task made me create a pythoh script that goes and searches for the file/folder you want starting from the path you desire.

Source Code:

# find.py

import os
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--file", type=str, help="File to search")
parser.add_argument("--folder", type=str, help="Folder to search")
parser.add_argument("--path", type=str, help="Path from where the search will start")
args = parser.parse_args()

search_file = args.file
search_folder = args.folder
search_path = '.'
if args.path and os.path.isdir(args.path):
search_path = args.path

for path, dirs, files in os.walk(search_path):
if search_file in files:
print(f'{os.path.join(path, search_file)}')
elif search_folder in dirs:
print(f'{os.path.join(path, search_folder)}')


Usage:

python3 find.py --file secretfile --path /home

Help:

python3 find.py --help


More Stories

Incomplete but Invaluable: My MERN Movie Web App Journey

In a world saturated with streaming platforms and movie databases, my love of movies fueled a desire to start on a creative journey—one that would take me deep into the heart of full-stack development. Join me as I unravel the highs, lows, and invaluable lessons learned while developing a MERN movie app.

Explore the kaleidoscopic world of coding creativity
Suraj Gaire