0

how do I avoid searching a folder? This script goes through every folder a searches it for your file, how do I avoid searching Applications? or only search the folders I tell it to. I've been trying for at least 3 hours

from PIL import Image
user_path = ("/Users/" + getpass.getuser())
FileName = input("file name, please, including the exsention: ")
print("working?")
for folder, sub_folder, files in os.walk(user_path):
    print(f"folder is {folder}")
    for sub_fold in sub_folder:
        print(f"sub folder is {sub_fold}")
        for f in files:
            print(f"file: {f}")
            if FileName == f:
                print("file found")
                print(os.path.abspath(os.path.join(root, name)))
richmail
  • 11
  • 6

3 Answers3

0

Create array with folders excluded. When loops entering into folder check is that folder name are in array created above. If it is just ignore.

KanekiSenpai
  • 122
  • 10
  • how ot make an array? is it jsut a list? if so then it dosnt work – richmail Jan 06 '21 at 02:03
  • You create array/list with folder names which will be exlcuded from the search. If file going into that folder (loop at that folder), You checking is that folder name in that list/array. If yes, You don't go into fool with subfolder/file. – KanekiSenpai Jan 06 '21 at 02:11
0

I made a sample code. Please check and respond to me.

import os

ext = ["a", "b", "c"] # I assume these are unnecessary folders.
for folder, sub_folder, files in os.walk(user_path):
    print(f"folder is {folder}")
    for sub_fold in sub_folder:
        if sub_fold in ext:
            continue
        else:      
            print(f"sub folder is {sub_fold}")
            for f in files:
                print(f"file: {f}")
                if FileName == f:
                    print("file found")
                    print(os.path.abspath(os.path.join(root, name)))
0

os.walk walks the entire directory tree, presenting the current directory, its immediate subfolders and its immediate files on each iteration. As long as you are walking top-down (the default) you can stop a subfolder from being iterated by removing it from the folders list. In this example, I made the blacklist a canned list in the source, but you could prompt for it if you'd like. On each folder iteration all you need to do is see if the wanted filename is in the list of file names in that iteration.

from PIL import Image
import getpass
import os

# blacklist folders paths relative to user_path
blacklist = ["Applications", "Downloads"]

# get user root and fix blacklist
# user_path = ("/Users/" + getpass.getuser())
user_path = os.path.expanduser("~")
blacklist = [os.path.join(user_path, name) for name in blacklist]
FileName = input("file name, please, including the exsention: ")
print("working?")
for folder, sub_folders, files in os.walk(user_path):
    # eliminate top level folders and their subfolders with inplace
    # remove of subfolders
    if folder in blacklist:
        del sub_folders[:]
        continue
    # in-place remove of blacklisted folders below top level
    for sub_folder in sub_folders[:]:
        if os.path.join(folder, sub_folder) in blacklist:
            sub_folders.remove(sub_folder)
    if FileName in files:
        print("file found")
        print(os.path.abspath(os.path.join(folder, FileName)))
tdelaney
  • 73,364
  • 6
  • 83
  • 116