---
title: How to search for files on your computer faster with Python
description: How to search for files on your computer faster with Python
---

[Skip to content](https://www.connysoderholm.com/blog/search-files-on-your-computer-faster-with-python#main-content)

![Conny-Söderholm](https://www.connysoderholm.com/hubfs/Conny-S%C3%B6derholm.svg)

- [Home](https://www.connysoderholm.com/)
- [Courses](https://www.connysoderholm.com/courses)
- [Articles](https://www.connysoderholm.com/blog)
- [Contact Me](https://www.connysoderholm.com/contact-me)

Open main navigation

Close main navigation

- [Home](https://www.connysoderholm.com/)
- [Courses](https://www.connysoderholm.com/courses)
- [Articles](https://www.connysoderholm.com/blog)
- [Contact Me](https://www.connysoderholm.com/contact-me)

 June 28, 2018

# How to search for files on your computer faster with Python

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm)

Share: [facebook-f icon](http://www.facebook.com/share.php?u=https://www.connysoderholm.com/blog/search-files-on-your-computer-faster-with-python) [linkedin-in icon](http://www.linkedin.com/shareArticle?mini=true&url=https://www.connysoderholm.com/blog/search-files-on-your-computer-faster-with-python) [Twitter icon](https://twitter.com/intent/tweet?url=https://www.connysoderholm.com/blog/search-files-on-your-computer-faster-with-python) [pinterest-p icon](http://pinterest.com/pin/create/link/?url=https://www.connysoderholm.com/blog/search-files-on-your-computer-faster-with-python) [envelope icon](mailto:?body=https://www.connysoderholm.com/blog/search-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](https://connysoderholm.com/file-dialog-box-tkinter/) 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](https://www.connysoderholm.com/blog/create-folders-from-a-text-file-line-by-line-with-python) and [how to read folder file contents into a text file](https://www.connysoderholm.com/blog/folder-file-contents-to-text-python) 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))
```

 

[Python](https://www.connysoderholm.com/blog/tag/python)

## Related posts

[Python](https://www.connysoderholm.com/blog/tag/python)

## [Create folders from a text file, line by line with Python](https://www.connysoderholm.com/blog/create-folders-from-a-text-file-line-by-line-with-python)

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm) 

 June 23, 2018

Create folders from a text file, line by line with Python - Do you sometimes need to create a lot...

[Read more](https://www.connysoderholm.com/blog/create-folders-from-a-text-file-line-by-line-with-python)

[Python](https://www.connysoderholm.com/blog/tag/python), [pypdf2](https://www.connysoderholm.com/blog/tag/pypdf2)

## [Rotate PDF files with PyPdf2 and Tkinter!](https://www.connysoderholm.com/blog/rotate-pdf-files-with-pypdf2-and-tkinter)

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm) 

 January 24, 2020

Rotating PDF files with PyPDF2 and Tkinter Introduction Sometimes we need simple tools to get the...

[Read more](https://www.connysoderholm.com/blog/rotate-pdf-files-with-pypdf2-and-tkinter)

[Python](https://www.connysoderholm.com/blog/tag/python)

## [How to read folder file contents into a text file with Python](https://www.connysoderholm.com/blog/folder-file-contents-to-text-python)

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm) 

 June 25, 2018

How to read folder file contents into a text file with Python - Do you need to take all existing...

[Read more](https://www.connysoderholm.com/blog/folder-file-contents-to-text-python)

[linkedin-in icon](https://www.linkedin.com/in/connysoderholm) [Follow us on Facebook](https://www.youtube.com/@connysoderholm1473)

- [Home](https://www.connysoderholm.com)
- [Courses](https://www.connysoderholm.com/courses)
- [Articles](https://www.connysoderholm.com/blog)
- [Contact Me](https://www.connysoderholm.com/contact-me)
- [Privacy Policy](https://www.connysoderholm.com/privacy-policy)
- [Disclaimer](https://www.connysoderholm.com/disclaimer)

---

[![Conny Söderholm Logo](https://www.connysoderholm.com/hubfs/Conny-S%C3%B6derholm.svg "Conny Söderholm Logo")](http://Conny%20Söderholm)

Copyright © 2026 Conny Söderholm

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Conny Söderholm",
    "url" : "https://www.connysoderholm.com/blog/author/conny-soderholm"
  },
  "dateModified" : "2023-07-31T15:24:26.885Z",
  "datePublished" : "2018-06-28T17:52:45.000Z",
  "headline" : "How to search for files on your computer faster with Python",
  "mainEntityOfPage" : {
    "@id" : "https://www.connysoderholm.com/blog/search-files-on-your-computer-faster-with-python",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://www.connysoderholm.com/hubfs/Conny-S%C3%B6derholm.svg"
    },
    "name" : "Conny Söderholm"
  }
}
```