How can I search a folder which it's name contains specifc character in Python

StackOverflow https://stackoverflow.com/questions/22162426

  •  19-10-2022
  •  | 
  •  

Question

I know I can use GLOB to Search Folder, but for example:

ssd = raw_input("input sth: ")
glob.glob('C:\Program Files\'+ssd)

this way only with exact folder name I can search the folder.

I saw something about using * but not sure how to use it.

I know the answers gonna be very easy but I'm a newbie so... please help.

Was it helpful?

Solution

As devnull commented, surround the input variable ssd with *:

glob.glob('C:\\Program Files\\*{}*'.format(ssd))

Side Note: You should escape backslashes as I did in above code, or use raw string literal.

glob.glob(r'C:\Program Files\*{}*'.format(ssd))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top