We need to walk through a file tree, going down into each subfolder. For now I am just logging the file path to the terminal. The source folder is hard coded, and it is in Windows format. But it should automatically work for other operating systems.
Traverse files
ChatGPT gave me code that you can use inline, but I actually wanted a function so found this on stack overflow which I like better. The ChatGPT code is in the image for this page. It basically does the same thing, but you would have to put your logic inside the loop that walks the code, which is not re-usable or clean.
It is also cleaner, as the walk_through_files() function does nothing except walk through the files. It is horribly indented, at this point my least favourite thing about Python. The last thing you want is your logic squished up in the right hand side of the code editor.
import os
root_directory = "C:\\Users\\*****\\Desktop\\Team Turkey Run 2019"
extensions = (".jpg", ".jpeg")
def walk_through_files(path, extensions):
for (dirpath, _, filenames) in os.walk(path):
for filename in filenames:
if filename.lower().endswith(extensions):
yield os.path.join(dirpath, filename)
for fname in walk_through_files(root_directory, extensions):
print(fname)
Code Walkthrough
- The os module provides a portable way of using operating system dependent functionality
- os.walk yields a 3-tuple
(dirpath, dirnames, filenames)
. - By replacing _ for dirnames, it indicates that we are not interested in them
- The first ‘for’ will first give the root folder, then it will go down each subfolder in turn.
- The second ‘for’ goes through each file in the folder
- The ‘if’ allows us to ignore any files we are not interested in.
- Note that endswith() magically works with an array of extensions, saving us another indentation.
Platform independent
The choice of the os library means this should work equally well on any system. It works on Windows, going first through the parent folder, and then going down each subfolder and its subfolders.