Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a barebones code that will synchronize two directories if they have the same structure within the directory.</p> <pre><code>import shutil import os #Assuming your folders are identical for synchronization purposes root_src_dir = "Path\To\Source" root_dst_dir = "Path\To\Dest" for src_dir, dirs, files in os.walk(root_src_dir): dst_dir = src_dir.replace(root_src_dir, root_dst_dir) if not os.path.exists(dst_dir): os.mkdir(dst_dir) for file_ in files: src_file = os.path.join(src_dir, file_) #dst_file = os.path.join(dst_dir, file_) #Decides whether or not to replace files in the destination if os.path.exists(os.path.join(root_dst_dir,dst_file)): #EDIT HERE. continue else: print "Copying", dst_file shutil.copy(src_file,os.path.join(root_dst_dir,dst_file)) #EDIT HERE </code></pre> <p>This will automatically create a "copy" of the source directory to the destination directory. It will create subdirectories that are missing and copy files in those specific locations to the destination directory only if the file does not yet exist in the destination. </p> <p>If you want make sure files are identitcal or not, then you'll probably want to look into <a href="http://docs.python.org/2/library/filecmp.html" rel="nofollow">filecmp</a> or hashes(below) to check if you've copied the file before.</p> <pre><code>import hashlib def cmpHash(file1,file2): """ Compares two files' hashes to determine duplicates. This doesn't work out so well, possibly due to different metadata""" hash1 = open(file1,'r').read() hash2 = open(file2,'r').read() #returns true if the files are the same - otherwise, false. return hashlib.sha512(file1).hexdigest() == hashlib.sha512(file2).hexdigest() </code></pre> <p>Example: (NO LONGER TRUE AFTER EDIT).</p> <pre><code>DriveA:\SomeDirectory\SourceDirectory\-Stuff- DriveB:\DestDirectory\-Stuff- #All -Stuff- from the SourceDirectory will be copied to DestDirectory, regardless of directories infront of Source/Dest Directory </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload