简体   繁体   中英

How to iterate through directory to find folders

I am trying to iterate through directory to find folders to change the path. At the moment I am using:

root = tk.Tk(); root.withdraw()  # close the tkinter root window
path = filedialog.askdirectory()  # open file dialogue box

to select the folder. After being selected a loop is run through that folder and scans all the images within it. However I have a lot of folders to scan through and was wondering if there was a way to scan through a directory and just find the folders so that they could be scanned automatically in python. So that for each folder it changed the path and this looped over all the folders. Any help would be much appreciated.

It sounds like you are looking for os.walk .

os.walk(top, topdown=True, onerror=None, followlinks=False)¶

Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM