1

since I'm fairly new to coding and python I can't solve a problem. I would like to create a code that would get exif info from .jpeg files and then create folders depending on the dates i found in exif and then move those pictures in their respective folders. Until now my code gets the dates and then creates folders(year then inside month then inside days). But i can't figure out how to move the pictures to their corresponding folders. This is my code. Any help would be appreciated.

import os
import sys
import PIL.Image
import PIL
from PIL import ExifTags
import datetime
from datetime import datetime
import os.path, time
from os import walk
import pathlib
import re


path=os.path.join(sys.argv[1])
destination=os.path.join(sys.argv[2])


#Pour creer la liste contenant les fichiers du dossier
def getListOfFiles(path):
    listOfFile=os.listdir(path)    
    allFiles=[]
    for i in listOfFile:
        fullPath=os.path.join(path, i)
        if os.path.isdir(fullPath):
            allFiles= allFiles+getListOfFiles(fullPath)
        else:
            allFiles.append(fullPath)
    return allFiles

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass

    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass

#print(getListOfFiles(path))
#print(len(getListOfFiles(path)))

#get metadata
foldertobemade=[]
extensions = ('.jpg')
for i in getListOfFiles(path):
    #print(i) #prints all the names of images
    ext = os.path.splitext(i)[-1].lower()
    #print(ext)
    if ext == extensions:
        #print(i)    
        img=PIL.Image.open(i)
        #print(img) #prints the image opened
        exif=img._getexif()
        #print(exif) #prints metadata
        try:
            if 36867 in exif:
                #print(exif[36867])
                foldertobemade.append(exif[36867])

        except Exception as e:
            pass 
#print(foldertobemade)        



    for dates in foldertobemade:
        #print(dates[:4])
        #print(dates[5:7])
        #print(dates[8:10])
        for i in getListOfFiles(path):
            if not os.path.exists(dates[:4]):   
                os.makedirs(dates[:4]) 

            if not os.path.exists(dates[:4]+ "/" + dates[5:7]):   
                os.makedirs(dates[:4]+ "/" + dates[5:7])

            if not os.path.exists(dates[:4]+ "/" + dates[5:7]+ "/" +dates[8:10]):   
                os.makedirs(dates[:4]+ "/" + dates[5:7]+ "/" +dates[8:10])

mid
  • 15
  • 4
  • See this :https://stackoverflow.com/questions/8858008/how-to-move-a-file-in-python – I_Al-thamary Nov 23 '19 at 16:52
  • I tried using shutil.move after the creation of the folder but i does not work – mid Nov 23 '19 at 16:54
  • Use after `os.makedirs` `import shutil` `shutil.move(destination, source)` – I_Al-thamary Nov 23 '19 at 16:55
  • do i have to use it after every os.makedirs or at the end of their creation? – mid Nov 23 '19 at 16:57
  • It doesn't matter but makes sure that you write the destination and source right. – I_Al-thamary Nov 23 '19 at 16:58
  • i should use my counter "i" since it has to move every jpg independently. Plus can i write shutil.move(sys.argv[1] ,sys.argv[2])? – mid Nov 23 '19 at 17:03
  • Yes, but make sure that they are in the same order of the destination and the source or make `destination=sys.argv[1]` and `source=sys.argv[2]` – I_Al-thamary Nov 23 '19 at 17:06
  • Use `path=os.path.join(sys.argv[1])` `destination=os.path.join(sys.argv[2])` as in your code `shutil.move(destination, path)` – I_Al-thamary Nov 23 '19 at 17:12
  • I can't seem to understand where to put the shutil.move and how to use my "i" since i want it to copy all the pictures based on their date of creation – mid Nov 23 '19 at 17:12
  • See this `import shutil, os` `files = ['file1.txt', 'file2.txt', 'file3.txt']` `for f in files:` `shutil.move(f, 'dest_folder')` – I_Al-thamary Nov 23 '19 at 17:15
  • What that does is just copy them to another folder it doesn't copy them to their corresponding folder(based on their date extracted from the exif) – mid Nov 23 '19 at 17:24
  • You need to change the destination to be the created folder. `dates[:4]` as an example. – I_Al-thamary Nov 23 '19 at 17:26
  • 1
    Oohh i see thank you – mid Nov 23 '19 at 17:28
  • first use `print()` to see values in variables. It helps to see problem. – furas Nov 23 '19 at 17:38

1 Answers1

0

I just add shutil.move(i, 'dest_folder') to your different destinations and it works.

import os
import sys
import PIL.Image
import PIL
from PIL import ExifTags
import datetime
from datetime import datetime
import os.path, time
from os import walk
import pathlib
import re
import shutil

path=os.path.join(sys.argv[1])
destination="/"


#Pour creer la liste contenant les fichiers du dossier
def getListOfFiles(path):
    listOfFile=os.listdir(path)
    allFiles=[]
    for i in listOfFile:
        fullPath=os.path.join(path, i)
        if os.path.isdir(fullPath):
            allFiles= allFiles+getListOfFiles(fullPath)
        else:
            allFiles.append(fullPath)
    return allFiles

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass

    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass

#print(getListOfFiles(path))
#print(len(getListOfFiles(path)))

#get metadata
foldertobemade=[]
extensions = ('.jpg')
for i in getListOfFiles(path):
    #print(i) #prints all the names of images
    ext = os.path.splitext(i)[-1].lower()
    #print(ext)
    if ext == extensions:
        #print(i)
        img=PIL.Image.open(i)
        #print(img) #prints the image opened
        exif=img._getexif()
        #print(exif) #prints metadata
        try:
            if 36867 in exif:
                #print(exif[36867])
                foldertobemade.append(exif[36867])

        except Exception as e:
            pass
#print(foldertobemade)



    for dates in foldertobemade:
        #print(dates[:4])
        #print(dates[5:7])
        #print(dates[8:10])
        for i in getListOfFiles(path):
            try:
                if not os.path.exists(dates[:4]) and  not os.path.exists(dates[:4]+"/"+i):
                    os.makedirs(dates[:4])

                    shutil.move(i,dates[:4])

                if not os.path.exists(dates[:4]+ "/" + dates[5:7]) and not os.path.exists(dates[:4]+"/"+i):
                    os.makedirs(dates[:4]+ "/" + dates[5:7])
                    shutil.move( i,dates[:4]+ "/" + dates[5:7])
                if not os.path.exists(dates[:4]+ "/" + dates[5:7]+ "/" +dates[8:10]) and not  os.path.exists(dates[:4]+"/"+i):
                    os.makedirs(dates[:4]+ "/" + dates[5:7]+ "/" +dates[8:10])
                    shutil.move(i,dates[:4]+ "/" + dates[5:7]+ "/" +dates[8:10])

            except shutil.Error as e:
                print(e)
I_Al-thamary
  • 3,385
  • 2
  • 24
  • 37
  • It shows an error in this line "shutil.move( i,dates[:4]+ "/" + dates[5:7])". The error is that a certain file isn't a file or directory – mid Nov 23 '19 at 19:50
  • Write it all as in your code, I run the code in my computer and it creates a folder with a year and inside it a month and inside the month day folder and then put the file there that contains the same date. – I_Al-thamary Nov 23 '19 at 20:35
  • Maybe the problem in the `\\`. Try it now. – I_Al-thamary Nov 23 '19 at 20:38
  • "FileNotFoundError: [Errno 2] No such file or directory: 'tidyImages/gps/DSCN0012.jpg'" this is the error i get. – mid Nov 23 '19 at 20:40
  • 1
    It works on windows but it doesn't work on ubuntu. Nevertheless the code works perfectly. Thank you! – mid Nov 23 '19 at 21:05
  • Change the `\\` – I_Al-thamary Nov 23 '19 at 21:09