I have a program that asks the user for a path and then searches for a filename entered by the user.
I have already tried os.listdir
, but it returns just the files and subdirectories directly inside the path.
It also does not search the subdirectories for the files.
I also don't know how to use os.walk()
method.
Any help on how to do that?
CodePudding user response:
You can use the glob.glob()
function to do the job.
Syntax:
glob.glob(path, recursive=True)
Example code:
import glob
path = "C:\\Users\\<username>\\Example"
names = glob.glob(path "\\*\\*.*", recursive=True)
This searches recursively in the directory and returns a list of the files found.
CodePudding user response:
Use pathlib : https://docs.python.org/3/library/pathlib.html?highlight=pathlib#module-pathlib
for PyFile in Path('.').glob('**/*.py'):
...