Skip to content

Instantly share code, notes, and snippets.

@lennymd
Created February 15, 2020 21:16
Show Gist options
  • Save lennymd/62120f258bdd9f43ad3c544274c00171 to your computer and use it in GitHub Desktop.
Save lennymd/62120f258bdd9f43ad3c544274c00171 to your computer and use it in GitHub Desktop.
How to get the paths for image files
# This script will search for all the
import glob, csv
# the name of your hard drive
hdName = "lennymartinezd"
# the name of the folder where all the POY images are on"
mainFolderName = "_photoExports"
mainFolderPath = "/Volumes/" + hdName + "/" + mainFolderName
# the different photo formats I expect would be used over the years
fileTypes = ["jpg", "JPG", "jpeg", "JPEG", "png", "PNG"]
# The single line of code is the same as the following extended code.
# It's called a list comprehension and is slightly more efficient
# queries = []
# for fileType in fileTypes:
# queries.append(mainFolderPath + "/*/*." + fileType)
queries = [mainFolderPath + "/*/*." + fileType for fileType in fileTypes]
# print(queries[0])
# From the above print, you may end up with a query that looks like this:
# /Volumes/lennymartinezd/_photoExports/*/*.jpg
# What we'll do now is use the glob module to run this query.
# Starting at the /Volumes/lennymartinezd/_photoExports
# it will go through every folder and any subfolder
# to find all .jpg files.
pictures = ["absoluteURL"]
for query in queries:
pictures.extend(glob.glob(query))
# At this point, you have a list of all the paths for all the pictures.
# If you try to save this as it is now to a CSV, you will 1 row
# with n columns. Let's flip things so you have 1 column with n rows.
pictures_col = []
for picture in pictures:
pictures_col.append([picture])
# That worked out nice because python will do the stuff on the right
# of the = before updating the pictures variable (on the left of the =)
# All that's left is to save. I always look at how i did it in an old
# project and change the names for what i need now:
with open("pictures.csv", "w") as myFile:
csv.writer(myFile, delimiter=",").writerows(pictures_col)
@lennymd
Copy link
Author

lennymd commented Feb 15, 2020

Here's a picture of my folder structure. This code will look through as many subfolders as it needs!

Screenshot 2020-02-15 16 07 11

@lennymd
Copy link
Author

lennymd commented Feb 15, 2020

You should be able to save the code to your desktop and then run it from there using the following terminal commands.
If python getFiles.py, use python3 getFiles.py
Screenshot 2020-02-15 16 20 27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment