Python Forum
Finding directory based on wildcard?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding directory based on wildcard?
#1
Hi.

I'm trying to create a function that will return a (windows) directory based on a wildcard.

For instance, my starting directory is C:\users\auser, and I would like to search through all (sub) directories under this directory and return any directory names that contain '*gain*' in the directory name.

Any advice would be appreciated.

Thanks.
Reply
#2
maybe something like this:
import os


def search_dir(starting_directory, string_to_search):
    directory_list = []
    for directory in [x[0] for x in os.walk(starting_directory)]:
        if string_to_search in os.path.basename(os.path.normpath(directory)):
            directory_list.append(directory)
    return directory_list

starting directory for you can be 'C:\users\auser' and string to search f.e. '*gain*'
Reply
#3
(Apr-24-2018, 01:41 PM)mlieqo Wrote: maybe something like this:
import os


def search_dir(starting_directory, string_to_search):
    directory_list = []
    for directory in [x[0] for x in os.walk(starting_directory)]:
        if string_to_search in os.path.basename(os.path.normpath(directory)):
            directory_list.append(directory)
    return directory_list

starting directory for you can be 'C:\users\auser' and string to search f.e. '*gain*'

That's excellent - thank you for the help!

Now I want to make sure I handle exceptions correctly. IE - there should always only be one directory returned. What would be the "correct" way to return an error if the len(directory_list) <> 1?

Thanks again.
Reply
#4
Have you considered using glob.glob()?
Reply
#5
(Apr-24-2018, 02:20 PM)jkimrey Wrote: Now I want to make sure I handle exceptions correctly. IE - there should always only be one directory returned. What would be the "correct" way to return an error if the len(directory_list) <> 1?
Well you can add another if statement and raise error if your len(directory_list) is not equal to 1, or simply just add assert statement before return - not sure exactly what is the 'correct' way. And also maybe think about doing it outside of the function, maybe you will want to have the list longer in some cases.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding the price based on industry and number of transactions chandramouliarun 0 917 Jul-26-2022, 07:36 PM
Last Post: chandramouliarun
  Reading Excel file and use a wildcard in file name and sheet name randolphoralph 6 7,153 Jan-13-2022, 10:20 PM
Last Post: randolphoralph
  pymongo wildcard query issue MikeAW2010 2 4,272 Jul-06-2021, 09:25 AM
Last Post: swag
  python script to get wildcard mask output in the following format techrichit 0 3,835 Aug-10-2018, 11:01 PM
Last Post: techrichit
  Problem with assigning directory of a file based on cell name mmaz67 3 2,834 Jul-04-2018, 08:40 AM
Last Post: Larz60+
  sqlite3 wildcard use problems lusticus 1 3,101 Apr-11-2018, 08:22 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020