Skip to content

Instantly share code, notes, and snippets.

@hughlilly
Last active August 5, 2021 23:19
Show Gist options
  • Save hughlilly/7119ee75393234b9726703cd1098498b to your computer and use it in GitHub Desktop.
Save hughlilly/7119ee75393234b9726703cd1098498b to your computer and use it in GitHub Desktop.
Uses Python's iglob iterator to work on files in sequence.
import glob
num_files = len(glob.glob('*.jpg'))
if num_files > 0:
print("Found {0} JPEG files".format(num_files))
jpeg_files = glob.iglob('*.jpg')
for count, filename in enumerate(jpeg_files, start=1):
print("----\n")
percent_complete = int((count * 100) / num_files)
print("File: {0} ({1} of {2}; {3}%).".format(
filename, count, num_files, percent_complete))
if count == num_files:
print("Working on {0}...".format(filename))
# do stuff
print("That was the last file.\n")
else:
print("Working on {0}...\n".format(filename))
# do stuff
else:
print("No .jpg files in current directory. Exiting.")
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment