Quand Python vient corriger un problème de script … Merci

Je me suis fait des doublons de fichiers dans mes photos, heureusement Python c’est simple et convivial :

import os
import sys
import re
import PIL
from PIL import Image

walk_dir = "./Nextcloud/Photos/"

print('walk_dir = ' + walk_dir)
print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))

for root, subdirs, files in os.walk(walk_dir):
    list_file_path = os.path.join(root, 'my-directory-check.txt')
    print('list_file_path = ' + list_file_path)
    with open(list_file_path, 'wb') as list_file:
        #for subdir in subdirs:
        #   print('\t- subdirectory ' + subdir)
        for filename in files:
            file_path = os.path.join(root, filename)
            # print('\t- file %s (full path: %s)' % (filename, file_path))
            base = os.path.splitext(filename)[0]
            if (base.endswith('_1')):
                    #print('\t- Doublon probable %s %s (full path: %s)' % (filename, base, file_path))
                    double = re.sub('_1$', '', base)
                    double = double + '.jpg'
                    double2 = re.sub('_1$', '', base)
                    double2 = double2 + '.JPG'
                    file_path_double = os.path.join(root, double)
                    file_path_double2 = os.path.join(root, double2)
                    if os.path.isfile(file_path_double):
                        #print('\t- Doublon OK %s & %s' % (file_path, file_path_double))
                        img1 = PIL.Image.open(file_path)
                        img2 = PIL.Image.open(file_path_double)
                        wid1, hgt1 = img1.size 
                        wid2, hgt2 = img2.size 
                        if (wid1 == wid2) and (hgt1 == hgt2):
                            #print('Meme resolution')
                            size1 = os.path.getsize(file_path)
                            size2 = os.path.getsize(file_path_double)
                            if (size1 > size2):
                                    print("%s > %s" % (file_path, file_path_double));
                                    os.remove(file_path_double);
                            else:
                                    print("%s < %s" % (file_path, file_path_double));
                                    os.remove(file_path);
                    elif os.path.isfile(file_path_double2):
                        img1 = PIL.Image.open(file_path)
                        img2 = PIL.Image.open(file_path_double2)
                        wid1, hgt1 = img1.size
                        wid2, hgt2 = img2.size
                        if (wid1 == wid2) and (hgt1 == hgt2):
                            #print('Meme resolution')
                            size1 = os.path.getsize(file_path)
                            size2 = os.path.getsize(file_path_double2)
                            if (size1 > size2):
                                    print("%s > %s" % (file_path, file_path_double2));
                                    os.remove(file_path_double2);
                            else:
                                    print("%s < %s" % (file_path, file_path_double2));
                                    os.remove(file_path);


Ensuite pour faire un clean des fichiers txt :

find ./Nextcloud/Photos/ -name my-directory-check.txt -exec /bin/rm -f {} \;

Vive les boulettes ..

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Time limit is exhausted. Please reload CAPTCHA.