0

I have an archived folder, named "dir1".
I have an new data folder, named "dir2".
I have a differences folder, named "dir3".
The data are jpgs, there are no sub folders.

dir1
1.jpg
2.jpg
3.jpg
4.jpg

dir2
1.jpg (edited file)
2.jpg (edited file)
3.jpg (same file as in dir1)
4.jpg (same file as in dir1)
5.jpg (new file)
6.jpg (new file)

I need to move the new, missing, edited and differing files to dir3.
The result would be:

dir3
1.jpg
2.jpg
5.jpg
6.jpg

Here is what i have:

from filecmp import dircmp

dcmp = dircmp('dir1', 'dir2')

def main(dcmp):
    for name in dcmp.diff_files, dcmp.right_only:
        print (name)

if __name__ == "__main__":
    main(dcmp)

The result is:

['2.jpg', '1.JPG']
['6.jpg', '5.jpg']

Insted of "print", how do i move these to dir3.
I've tried "shutil" and "os.rename", but i cant get it to work.


Information from "filecmp" and "dircmp".

import filecmp

filecmp.dircmp('dir1', 'dir2').report()

The result is:

diff dir1 dir2
Only in dir2 : ['5.jpg', '6.jpg']
Identical files : ['3.JPG', '4.JPG']
Differing files : ['1.JPG', '2.jpg']

This is just fyi.

Mobs
  • 15
  • 6

2 Answers2

0

Here is a reference to answer on how to create a new directory and move files to it.

0

It seems like you already have the part to make the list of files that you want to move.

import shutil

for file in file_list:
    shutil.move(file, dir3) # shutil.copy if you want

Note: file should be the path to the file and dir3 can be a directory or file path if you want to change the name or the like.

it's-yer-boy-chet
  • 1,917
  • 2
  • 12
  • 21
  • I did this, and i get an error: from filecmp import dircmp import shutil def print_diff_files(dcmp): for name in dcmp.diff_files, dcmp.right_only: shutil.move(name, 'dir3') dcmp = dircmp('dir1', 'dir2') if __name__ == "__main__": print_diff_files(dcmp) – Mobs Jun 06 '17 at 18:26
  • You're passing arguments for path that aren't working. Try being more explicit in the path. e.g. instead of `name`, `os.path.join(r'C:\user\dir2', name)`. It's most likely an issue with your assumption of relative path. Also, your example is super convoluted with unnecessary functions etc, please try to reduce it to the relevant information. – it's-yer-boy-chet Jun 07 '17 at 17:52
  • I've change question to be less convoluted, i didnt want to leave something out... I need this optimal for comparing large amounts of data. – Mobs Jun 08 '17 at 18:39