canadadry
canadadry

Reputation: 8443

How do I compare 2 directories dir1 and dir2 and only copy differences in python?

Given 2 directories:

dir1
 |
 +----A
 |
 +----B

dir2
 |
 +----A (changed)
 |
 +----B (no change)
 |
 +----C (added)

I want to write a python script which will detect and make the following changes:

My idea is to obtain a md5 checksum for all the files. Am I going in the right direction ?

Upvotes: 0

Views: 473

Answers (2)

shadyabhi
shadyabhi

Reputation: 17234

Have a look at the filecmp module.

http://docs.python.org/library/filecmp.html

It also has a function to compare directories.

class filecmp.dircmp(a, b[, ignore[, hide]])

Upvotes: 4

orlp
orlp

Reputation: 117681

If the files are big then using MD5 checksums is certainly sane.

Make sure to check for the "modified" timestamps to always copy over the newest file.

Upvotes: 1

Related Questions