I am trying to retrieve all the files in a folder with a specific extension. But the problem is that I dont know how many sub-directories can be in there..
So basically something like:
------ filex.py
---folder1 -------- filezs.py
root ------- folder2
--- file.py ----- fileabc.py
--- file.txt
and so on
So basically I want to write a function which does the following:
def get_files(root,files_of_type):
return dict{key = file_name: value = path}
So for example if I want to get all the file and their path of extension py
then this will be like:
root = "/path/to/root"
files_of_type = [".py"]
returns {"filex.py": "/path/to/root/folder1".. and so on}
Any suggestions
Thanks
You can do that with
os.walk. For each level of directory it will give you the directory path, a list of any subdirectory names, and a list of any file names in that directory. From there you can assemble the dictionary to return.