Skip to content

How to search for files on your computer faster with Python

How to search for files on your computer faster with Python

When I use the Windows Explorer search while looking for particular files on the network drive, I usually go for a coffee while the search is running. It takes painfully long to search for files and it is just frustrating when you cannot find files after a 10 min search. What you can do instead is to write some code and let Python do the searching for you.

It took me 0 minutes and 21.0 seconds to search folder contents for the letter "a" in my whole C: drive with 130 GB occupied disk space.  I Found 247588 search results for a. Not that useful search of course, but it shows how fast you can do searches.

Let's have a look at the code below. We start by asking the user where the directory we want to search is. If the user presses cancel on the askdirectory filedialog we exit the application. We then add a slash to the end of the folder name if needed. Next, we get the folder path with the os.path.dirname method.  We then get the preceding folder path and the name of the search folder with os.path.split.

Next, we perform some error checking in case the folder name is blank and also ask the user what to search for. We then start the clock with time.time() so we know how long the search took. We then create the search contents file. Remember to put some encoding in or you might get an UnicodeError, I use utf-8. The os.walk module walks through each folder in the directory.

the script is finished, now we just have to give some information to the user. It will save the search file in the same folder as the script is in. If you want more information about dealing with folder contents, I have two other blog posts about creating folders from a text file and how to read folder file contents into a text file with Python. Happy coding!

The code

#! python3

# -*- coding: utf-8 -*-

"""
By: Conny Söderholm

Reads the search results of a specified folder or drive
into text file.

In Python, there are a number of functions in the os.path
module that change forward slashes in a string to the
appropriate filename separator for the platform that you
are on. One of these function is os.path.normpath()
The trick is to enter all of your filename strings using
forward slashes, and then let os.path.normpath() change
them to backslashes for you, this way.

myDirname = os.path.normpath("c:/aDirname/")

"""

import os
import time
import tkinter as tk
from tkinter.filedialog import askdirectory

tk.Tk().withdraw() # Close the root window

folder_to_read = askdirectory()

if folder_to_read == "":
print("Exiting program")
quit()

if folder_to_read[-1] != '/':
folder_to_read += '/'

folder_path = os.path.dirname(folder_to_read)

path,folder_name = os.path.split(folder_path)

if folder_name is "":
folder_name = folder_to_read
if folder_name[-2:] == ':/':
folder_name = folder_name[:-2]

search_string = ""
while search_string is "":
search_string = input("Enter the name of the file(s) you want to search for:\n").lower()

search_results_found = 0
print("starting to search for the files...")

start_time = time.time()

with open(folder_name + " folder_search_results.txt", "w", encoding="utf-8") as a:
a.write("Searches found for " + search_string + ":\n")
for search_path, subdirs, files in os.walk(os.path.normpath(folder_to_read)):
for filename in files:
f = os.path.join(search_path, filename)
if search_string in f.lower():
search_results_found += 1
a.write( f + os.linesep)

seconds = time.time() - start_time

m, s = divmod(seconds, 60)
h, m = divmod(m, 60)

print("{:.0f} minutes and {:.1f} seconds used for searching folder contents for {}".format( m, s, search_string ))
print("Found {} search results for {}".format(search_results_found, search_string))