-1

I have looked through multiple websites multiple times and no other posts can find specifically what I am looking for.

The general idea is for the program to search through the entire computer, skipping any folders which deny access or in other ways are inaccessible, for a specific file and then return its location. E.g:

def findFile(targetFile):
    targetLocation = searchFor(targetFile) in drives.All

OUTPUT:

targetLocation = "D:\SteamLibrary\steamapps\common\Grand Theft Auto V"

OR:

This file exists in two locations:
"D:\SteamLibrary\steamapps\common\Grand Theft Auto V" and "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V"
Please specify which:

Thanks

NOTE: I have seen the questions/1724693/find-a-file-in-python and it does not answer my problem. I need something which will handle duplicates by asking you to pick which not putting it in an array

import os
target_filename = "GTA 5.exe"
start_folder = "C:/" #It needs to search every drive (C: D: E: F: etc)
for root, dirs, files in os.walk(start_folder):
    if target_filename in files:
        print(root + '\\' + target_filename )

output looks like this:

Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)]     on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
================== RESTART: C:/Users/Redacted/Desktop/test.py ==================
>>> 
0rdinal
  • 641
  • 4
  • 17

1 Answers1

0

If you know the filename (assumed to be target_filename) and the starting folder (assumed to be start_folder) you want to use os.walk to find duplicates:

import os

for root, dirs, files in os.walk(start_folder):
    if target_filename in files:
        print(root + '\\' + target_filename )
mrCarnivore
  • 4,638
  • 2
  • 12
  • 29