I currently have a class that defines three functions that extract the date and device name from images in a specified directory. 1 The function then copies the images to new file directories based on the date and device name. I need to collect these new directories in a list so I can loop through the directories and extract the individual images to process them. I have to process them after they're sorted in order to keep them separated when I send the data I've extracted to Excel. I tried making a list of these directories in the function by appending the directories as they're being created. When printed inside the function, all of the directories show up, however, if I print it outside the function, the list is blank.
I created the list outside of the class and then assigned the list as a global variable inside the function as was suggested here, however, the list still prints blank outside the function.
newpath = []
class imageSorter:
global newpath
newpath = []
def sort:
for fname in self.images:
with PIL.Image.open(os.path.join(self.dirname,fname) as img:
exif=img._getexif()
ts=self.preprocess_exif(exif[306])
date=ts.split(' ')[0]
manuf=self.preprocess_exif(exif[271)]
device=self.preprocess_exif(exif[272])
merged=manuf+' '+device
year =datetime.dateime.strptime(date, '%Y:%m:%d').strftime('%Y')
month =datetime.dateime.strptime(date, '%Y:%m:%d').strftime('%b')
date =datetime.dateime.strptime(date, '%Y:%m:%d').strftime('%d')
if not os.path.isdir(os.path.join(year,month,date)):
os.mkdir(os.path.join(merged,year,month,date))
shutil.copy(os.path.join(self.dirname,fname),os.path.join(merged,year,month,date,fname)))
newpath.append(os.path.join(merged,year,month,date))
1. This code was written by github user cw-somil