Find all the files on your computer

Find every file fast

If you want to find all the file on your computer with a word in it or with and extension, run the following python code. It uses the os.walk function that returns:

  • r = root directory
  • d = a list with the subdirectories in the root directory
  • f = a list with all the files in the root directory

After it returns these data, it goes into all the subdirectories and returns again r,d and f of each subdirectory.

This way you can check all folders and files that are in a hierarchy of files. If you start from C:\\, you will go through all the files of the hard drive… so it can take some time, but it is very fast. It is more suitable than the search system of the windows os, I think, and it is customable to our specific needs.

import os


counter = 0
print("If you want all the excel file, for example write .xlsx")
inp = input("What are you looking for?:> ")
thisdir = os.getcwd()
for r, d, f in os.walk("C:\\"): # change the hard drive, if you want
    for file in f:
        filepath = os.path.join(r, file)
        if inp in file:
        	counter += 1
        	print(os.path.join(r, file))
print(f"trovati {counter} files.")

Store in a txt files all the path to files of a type

One thing that you could find useful, is to store all the files you are interested in, into a text file, so that you can have them ‘on paper’ for any purpose you could need them.

You can find an example of this code here:

import os

def searchfiles(extension='.ttf', folder='H:\\'):
    "Create a txt file with all the file of a type"
    with open(extension[1:] + "file.txt", "w", encoding="utf-8") as filewrite:
        for r, d, f in os.walk(folder):
            for file in f:
                if file.endswith(extension):
                    filewrite.write(f"{r + file}\n")

# looking for png file (fonts) in the hard disk H:\
searchfiles('.png', 'H:\\')

Copy all the files of a type in one folder! Your files in order.

Another use could be to take all the file (pdf files for example) that you have in different folders and copy them into one folder, so that you can see all of them together without the need to go searching into every folder, not remembering if you got more of the same files or files for the same topic in different folders and so on.

import os
import shutil
from path import path

destination = "F:\\file_copied"
# os.makedirs(destination)

def copyfile(dir, filetype='pptx', counter=0):
    "Searches for pptx (or other - pptx is the default) files and copies them"
    for pack in os.walk(dir):
        for f in pack[2]:
            if f.endswith(filetype):
                fullpath = pack[0] + "\\" + f
                print(fullpath)
                shutil.copy(fullpath, destination)
                counter += 1
    if counter > 0:
        print('-' * 30)
        print("\t==> Found in: `" + dir + "` : " + str(counter) + " files\n")

for dir in os.listdir():
    "searches for folders that starts with `_`"
    if dir[0] == '_':
        # copyfile(dir, filetype='pdf')
        copyfile(dir, filetype='txt')

These are some of the possible use you can do with this useful function os.walk. It can look a bit strange at first for all the data it returns, but you will appreciate it just for this reason. It will make you save a lot of times.

Read about how to find files on the computer in this post of my blog.

Utilities


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.