Compare files in two directories and copy duplicates into a third directory - python

0

I have the following situation:

A directory with more than 3,000 files .jpg of which were copied half of the files to another directory, but after a problem in the computer the copied files were corrupted. Then I created a code to access the two directories, the source and what contains the corrupted files. the purpose is to take the files of the same name in both directories and copy the files from the source to a third folder. Note: the source files are in subdirectories.

The files are named this way:

0.jpg; 1.jpg; 2.jpg;...;101.jpg...

My code:

import os
import glob
import shutil

path_good = 
glob.glob("C:\dados\origem\*") # caminho dos arquivos originais
origem_list = []
for _ in path_good:
    path = _   

    for file in os.listdir(path):
         origem_list.append(file)

path_bad = "C:\dados\bad" # caminho dos arquivos corrompidos
bad_list= []
for file2 in os.listdir(path_bad):
    bad_list.append(file2)

for i in os.listdir(path_bad):
    for j in origem_list:
        if i==j:
           # print(j)
            shutil.copy(j, 
                      "C:\dados\nova_pasta")# caminho destino das cópias

When I run the code, it only copies the first 20 files. and displays this error:

  

FileNotFoundError: [Errno 2] No such file or directory: '66 .jpg '

In this case the next file would be just 66.jpg.

Where am I going wrong, does anyone help?

    
asked by anonymous 17.08.2018 / 15:05

0 answers