Compare files with a zip list and figure out if it is larger
Question: I want to compare the files of two zip folders. Copy only if the magnitude is
greater when there is already a zip at the end when missing an equal name,
copy the file. Only Name is compared not a date: es--> Campobasso[CB]-Molise

Folder DirTemp ZIP: Campobasso[CB]-Molise__02-02-2016.zip

Folder DirArc ZIP: Foggia[FG]-Puglia__22-01-2016.zip
Roma[RM]-Lazio__20-01-2016.zip

Folder DirArcScartati, They are the zip that if found and are smaller are put
another folder

This is my code, but work partially, I can not to copy the file (if not exist)
at the end of the control, with list.

    
    
    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    
    import os,glob,shutil
    
    DirTemp = "/var/www/vhosts/anon_ftp/incoming/"
    DirArc = "/var/www/vhosts/settings/BackupDTT/"
    DirArcScartati = "/var/www/vhosts/settings/BackupDTT_scartati/"
    ExtFile = ".zip"
    
    
    def ControlFile():
        # Controllo i nuovi file zip
        listnew=[]
        #print "Avvio copia"
        for name in glob.glob(DirTemp + "*" + ExtFile):
            listnew.append((name.replace(DirTemp,"").replace(ExtFile,"").split("__")[0],name))
            #print "Nome: "+ str(listnew) 
    
    
        for oldname in glob.glob(DirArc + "*" + ExtFile):
            #print "Setting Esistente: "+oldname 
            namesplit = oldname.replace(DirArc,"").replace(ExtFile,"").split("__")[0] 
            for newname in listnew:
                #print "New Nome: "+str(newname[0]) 
                print namesplit
                if namesplit == newname[0]:
                    if os.path.getsize(newname[1]) >= os.path.getsize(oldname):
                        print ("trasferire file" + newname[1] + " >>> " + oldname)
                        shutil.copy2(newname[1],DirArc)
                        os.remove(oldname)
                        #os.remove(newname[1])
                        break
                    elif os.path.getsize(newname[1]) <= os.path.getsize(oldname):
                        print ("File più piccolo---\nFileNuovo: " + str(os.path.getsize(newname[1])) + " FileVecchio: " + str(os.path.getsize(oldname)))
                        shutil.copy2(newname[1],DirArcScartati)
                        #os.remove(newname[1])
                        break
            else:
                for newname in listnew:
                    print ("Nuova città trasferisco il file: " + newname[1])
                    shutil.copy2(newname[1],DirArc)
                    #os.remove(newname[1])
                    break
    
    ControlFile()
    


Answer: The following approach might be a bit easier to follow:

    
    
    def get_file_dictionary(folder):
        """ Return a dictionary of zip files in the given folder as tuples in the form: (basename, full path, size) """
        return {os.path.splitext(os.path.split(x)[1])[0].split('__')[0] : (x, os.path.getsize(x)) for x in glob.glob(folder + '*.zip')}
    
    DirTemp = "/var/www/vhosts/italysat.eu/anon_ftp/incoming/"
    DirArc = "/var/www/vhosts/italysat.eu/settings.italysat.eu/BackupDTT/"
    DirArcScartati = "/var/www/vhosts/italysat.eu/settings.italysat.eu/BackupDTT_scartati/"    
    
    incoming = get_file_dictionary(DirTemp)
    existing = get_file_dictionary(DirArc)
    
    for base_name_inc, (full_name_inc, size_inc) in incoming.items():
        try:
            full_name_exist, size_exist = existing[base_name_inc]
    
            if size_inc > size_exist:
                print "Transfer {} -> {}".format(full_name_inc, full_name_exist)
                os.remove(full_name_exist)
                shutil.copy2(full_name_inc, full_name_exist)
            else:
                discard = os.path.join(DirArcScartati, os.path.split(full_name_inc)[1])
                print "Discard {} -> {}".format(full_name_inc, discard)
                shutil.copy2(full_name_inc, discard)
        except KeyError, e:
            new_entry = os.path.join(DirArc, os.path.split(full_name_inc)[1])
            print "Transfer new {} -> {}".format(full_name_inc, new_entry)
    
        os.remove(full_name_inc)
    

It first creates to dictionaries containing all of the entries in the incoming
folder and the archive folder. It then iterates over the incoming dictionary
to see if an entry is in the archive. If it is, it checks the two sizes, if
not it copies the new entry.

The dictionaries are stored with the key being your base name (without dates)
and the values being the full path name and the file size.

