2017-04-25 119 views
-1

我寫了一個Python腳本來比較的目錄腳本比較目錄

要求是他們的兩個文件

directory1中有內容

1.txt 
2.txt 
3.txt 
4.txt 
6.txt 
7.txt 
9.txt 

目錄2有內容

1.txt 
2.txt 
3.txt 
4.txt 
5.txt 
6.txt 
7.txt 
8.txt 
9.txt 

目錄2中的所有文件必須替換爲目錄1

例如directory1中的輸出應該與內容所代替內directory2

1.txt 
2.txt 
3.txt 
4.txt 
5.txt 
6.txt 
7.txt 
8.txt 
9.txt 

劇本我已經是

import os, sys 
import filecmp 
import re 
from distutils import dir_util 
import shutil 
holderlist=[] 

def compareme(dir1, dir2): 
    dircomp=filecmp.dircmp(dir1,dir2) 
    only_in_one=dircomp.left_only 
    diff_in_one=dircomp.diff_files 
    dirpath=os.path.abspath(dir1) 
    [holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in only_in_one] 
    [holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in diff_in_one] 
    if len(dircomp.common_dirs) > 0: 
     for item in dircomp.common_dirs: 
      compareme(os.path.abspath(os.path.join(dir1,item)), os.path.abspath(os.path.join(dir2,item))) 
     return holderlist 
     print holderlist 
def main(): 
if len(sys.argv) > 3: 
    dir1=sys.argv[1] 
    dir2=sys.argv[2] 
    dir3=sys.argv[3] 
else: 
    print "Usage: ", sys.argv[0], "currentdir olddir difference" 
    sys.exit(1) 

if not dir3.endswith('/'): dir3=dir3+'/' 

source_files=compareme(dir1,dir2) 
dir1=os.path.abspath(dir1) 
dir3=os.path.abspath(dir3) 
destination_files=[] 
new_dirs_create=[] 
for item in source_files: 
    destination_files.append(re.sub(dir1, dir3, item)) 
for item in destination_files: 
new_dirs_create.append(os.path.split(item)[0]) 
for mydir in set(new_dirs_create): 
    if not os.path.exists(mydir): os.makedirs(mydir) 

copy_pair=zip(source_files,destination_files) 
for item in copy_pair: 
    if os.path.isfile(item[0]): 
    shutil.copyfile(item[0], item[1]) 

if __name__ == '__main__': 
main() 

但它不是替換文件如預期

+1

您能否介紹一下到目前爲止您嘗試過哪些故障排除步驟以及結果是什麼? – glibdud

+0

考慮到你使用'print',這看起來更像是python-2.x。你可否確認? –

+0

您的腳本不能將Directory2的內容複製到Directory1並覆蓋?如果不是,請提供更多示例。 –

回答

0
  1. 刪除中的等位文件目錄1

    path = Directory1 
        fnames = os.listdir(path) 
        for fname in fnames: 
         fpath = os.path.join(path, fname) 
         if not stat.S_ISDIR(os.lstat(fpath).st_mode): 
          os.remove(fpath) 
    
  2. 移動所有文件從Directory2

    path = Directory2 
    fnames = os.listdir(path) 
    for fname in fnames: 
        source_fpath = os.path.join(path, fname) 
        if not stat.S_ISDIR(os.lstat(source_fpath).st_mode): 
         target_fpath = os.path.join(Directoy1, fname) 
         os.rename(source_fpath, target_fpath) 
    
0

那麼它不是清楚爲什麼你使用dir3,但我會跳過它,因爲我認爲我們的目標是轉移差異在dir1。如果你想將所有文件從dir2移動到dir1我也給出瞭解決方案。

import os 
import sys 
import shutil 

if __name__ == "__main__": 
    if len(sys.argv) > 3: 
     dir1=sys.argv[1] 
     dir2=sys.argv[2] 
    else: 
     print "Usage: ", sys.argv[0], "currentdir olddir" 
     sys.exit(1) 

    dir1_list = os.listdir(dir1) 
    dir2_list = os.listdir(dir2) 

    if dir1_list != dir2_list: 
     # If you want the different files in dir1 do this: 
     diff = list(set(dir2_list) - set(dir1_list)) 

     # If you want to move dir2 to dir1 do this: 
     diff = dir2_list 

     for file in diff: 
      shutil.move(os.path.join(dir2, file), os.path.join(dir1, file))