How it works...

We shall make a class called FileList; it will make use of the Photo class to manage
the photos within a specific folder. There are two main steps for this: we first need to find all the images within the folder, and then generate a list containing both the filename and the photo date. We will use this information to generate new subfolders and move the photos into these folders.

When we create the FileList object, we will create the list using listFileDates(). We will then confirm that the folder provided is valid and use os.listdir to obtain the full list of files within the directory. We will check that each file is a JPEG file and obtain each photo's date (using the function defined in the Photo class). Next, we will add the filename and date as a tuple to the self.photo_namedates list.

Finally, we will use the built-in sorted function to place all the files in order of their date. While we don't need to do this here, this function would make it easier to remove duplicate dates if we were to use this module elsewhere.

The sorted function requires the list to be sorted, and, in this case, we want to sort it by the date values:

   sorted(self.photo_namedates,key=lambda date: date[DATE])

We will substitute date[DATE] with lambda date: as the value to sort by.

Once the FileList object has been initialized, we can use it by calling genFolders(). First, we convert the date text into a suitable format for our folders (YYYYMMDD), allowing our folders to be easily sorted in order of their date. Next, it will create the folders within the current directory if they don't already exist. Finally, it will move each of the files into the required subfolder.

We end up with our FileList class that is ready to be tested:

Operations

Description

__init__(self,folder)

This is the object initializer.

getPhotoNamedates(self)

This returns a list of the filenames of the dates of the photos.

listFileDates(self)

This creates a list of the filenames and dates of the photos in the folder.

genFolders(self)

This creates new folders based on a photo's date and moves the files into them.

 

The properties are listed as follows:

Properties

Description

self.folder

The folder we are working with.

self.photo_namedates

This contains a list of the filenames and dates.

The FileList class encapsulates all the functions and the relevant data together, keeping everything in one logical place:

Tkinter filediaglog.askdirectory() is used to select the photo directory

To test this, we use the Tkinter filedialog.askdirectory() widget to allow us to select a target directory of pictures. We use app.withdrawn() to hide the main Tkinter window since it isn't required this time. We just need to create a new FileList object and then call genFolders() to move all our photos to new locations!

Two additional flags have been defined in this script that provide extra control for testing. DEBUG allows us to enable or disable extra debugging messages by setting them to either True or False. Furthermore, FOLDERSONLY, when set to True, only generates the folders and doesn't move the files (this is helpful for testing whether the new subfolders are correct).

Once you have run the script, you can check if all the folders have been created correctly. Finally, change FOLDERSONLY to True, and your program will automatically move and organize your photos according to their dates the next time. It is recommended that you
only run this on a copy of your photos, just in case you get an error.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.97.202