Recently in Python development, I studied the script written by the technology Daniel and optimized it in his script. Optimization scripts have been tested or very useful, if you think it is good to directly get production use it.
Put the code directly first:
---------------------------------------------------------------------------------------------------------
Import re,shutil,os,sys,filecmp
Difffileslist = []
def commpare (DIR1,DIR2):
CMPOBS=FILECMP.DIRCMP (DIR1,DIR2)
Dir1_only=cmpobs.left_only
Dir1_diff=cmpobs.diff_files
[Difffileslist.append (Os.path.join (Os.path.abspath (Dir1), a)) for a in Dir1_diff]
[Difffileslist.append (Os.path.join (Os.path.abspath (Dir1), a)) for a in dir1_only]
If Len (cmpobs.common_dirs) > 0:
For a in Cmpobs.common_dirs:
Commpare (Os.path.abspath (Os.path.join (dir1,a)), Os.path.abspath (Os.path.join (dir2,a)))
Return difffileslist
def main ():
If Len (SYS.ARGV) < 3:
Print "Plese use%s sourcedir backdir"% sys.argv[0]
Sys.exit ()
Else
Dir1=os.path.abspath (Sys.argv[1])
Dir2=os.path.abspath (Sys.argv[2])
Ifmakedir = True
While Ifmakedir:
Difffileslist = []
DestinationFiles = []
Ifmakedir=false
Sourcefiles=commpare (DIR1,DIR2)
For a in SourceFiles:
Destinationfile=re.sub (Dir1,dir2,a)
Destinationfiles.append (Destinationfile)
If Os.path.isdir (a):
If not os.path.exists (destinationfile):
Os.makedirs (Destinationfile)
Print "Make dir%s"% Destinationfile
Ifmakedir=true
DestinationFiles = []
Sourcefiles=[]
Sourcefiles=commpare (DIR1,DIR2)
[Destinationfiles.append (Re.sub (dir1,dir2,a)) for a in sourceFiles]
For a, b in Zip (sourcefiles,destinationfiles):
If Os.path.isfile (a):
Print "Copy file%s to%s"% (A, B)
Shutil.copyfile (A, B)
#print Sourcefiles,destinationfiles
if __name__ = = ' __main__ ':
Main ()
---------------------------------------------------------------------------------------------------------
Look directly at the effect:
[Email protected] tmp]# pwd
/tmp
[Email protected] tmp]# tree testsyncfile/
testsyncfile/
├──destinationfiles
└──sourcefiles
├──file
└──test1
├──file1
└──test2
├──file2
└──test3
5 directories, 3 files
[email protected] tmp]# python syncfile.py/tmp/testsyncfile/sourcefiles testsyncfile/destinationfiles/
Make Dir/tmp/testsyncfile/destinationfiles/test1
Make Dir/tmp/testsyncfile/destinationfiles/test1/test2
Make Dir/tmp/testsyncfile/destinationfiles/test1/test2/test3
Copy File/tmp/testsyncfile/sourcefiles/file To/tmp/testsyncfile/destinationfiles/file
Copy File/tmp/testsyncfile/sourcefiles/file To/tmp/testsyncfile/destinationfiles/file
Copy File/tmp/testsyncfile/sourcefiles/test1/file1 To/tmp/testsyncfile/destinationfiles/test1/file1
Copy File/tmp/testsyncfile/sourcefiles/file To/tmp/testsyncfile/destinationfiles/file
Copy File/tmp/testsyncfile/sourcefiles/test1/file1 To/tmp/testsyncfile/destinationfiles/test1/file1
Copy File/tmp/testsyncfile/sourcefiles/test1/test2/file2 To/tmp/testsyncfile/destinationfiles/test1/test2/file2
Copy File/tmp/testsyncfile/sourcefiles/file To/tmp/testsyncfile/destinationfiles/file
Copy File/tmp/testsyncfile/sourcefiles/test1/file1 To/tmp/testsyncfile/destinationfiles/test1/file1
Copy File/tmp/testsyncfile/sourcefiles/test1/test2/file2 To/tmp/testsyncfile/destinationfiles/test1/test2/file2
Copy File/tmp/testsyncfile/sourcefiles/file To/tmp/testsyncfile/destinationfiles/file
Copy File/tmp/testsyncfile/sourcefiles/test1/file1 To/tmp/testsyncfile/destinationfiles/test1/file1
Copy File/tmp/testsyncfile/sourcefiles/test1/test2/file2 To/tmp/testsyncfile/destinationfiles/test1/test2/file2
[Email protected] tmp]# tree testsyncfile/
testsyncfile/
├──destinationfiles
│├──file
│└──test1
│├──file1
│└──test2
│├──file2
│└──test3
└──sourcefiles
├──file
└──test1
├──file1
└──test2
├──file2
└──test3
8 Directories, 6 files
----------------------------------------------------------------------------------------------------------
A simple description of the script:
Simple instructions under the script, the script to the source data and the destination data to do the comparison, there are different files to replace and add to the directory to do new. But for the destination data redundant directories and files, the script good processing. People who want to use scripts can be used flexibly based on their own scenarios.
Just started to write a blog, write more hasty, follow-up will share more vivid articles!
For this article you have questions or suggestions welcome message.
This article is from the "Running Horse" blog, please make sure to keep this source http://5673807.blog.51cto.com/5663807/1922226
Python Synchronization files