標籤:python 練習
#!/usr/bin/env pythonimport osimport sysdef get_Curlist(path): cur_Files = os.listdir(path) all_Files = [] for file_Name in cur_Files: full_Name = os.path.join(path,file_Name) all_Files.append(full_Name) if os.path.isdir(full_Name): next_Levelfiles = get_Curlist(full_Name) all_Files.extend(next_Levelfiles) return all_Filesitem_Path = ‘/usr/local/webapp/document‘file_Path = get_Curlist(item_Path)item_Newpath = ‘/usr/local/webapp/document_newdocument‘newfile_Path = get_Curlist(item_Newpath)def add_File(): add_List=[] for num in range(0,len(newfile_Path)): renewfile_Path=newfile_Path[num].replace(‘_newdocument‘,‘‘) if renewfile_Path not in file_Path: add_List.append(renewfile_Path) return add_Listadd_Filelist = add_File()file_Add = open(‘/tmp/python_Addfile‘,‘w‘)file_Add.truncate()for num in range(0,len(add_Filelist)): file_Add = open(‘/tmp/python_Addfile‘,‘a‘) file_Add.write(add_Filelist[num]+‘\n‘)file_Add.close()def del_File(): del_File=[] for num in range(0,len(file_Path)): refile_Path=file_Path[num].replace(item_Path,item_Path+‘_newdocument‘) if refile_Path not in newfile_Path: del_File.append(refile_Path) return del_Filedel_Filelist = del_File()file_Add = open(‘/tmp/python_Delfile‘,‘w‘)file_Add.truncate()for num in range(0,len(del_Filelist)): file_Add = open(‘/tmp/python_Delfile‘,‘a‘) file_Add.write(del_Filelist[num]+‘\n‘)file_Add.close()
指令碼主要就是對比document和document_newdocument,通過replace修改擷取到的檔案路徑,對比另一個目錄的檔案路徑來判斷檔案是否兩邊都存在.
其中的Addfile是指document裡面沒有但是在document_newdocument裡面有的檔案
Delfile是指document裡面有但是在document_newdocument裡面沒有的檔案
本文出自 “空の心” 部落格,請務必保留此出處http://songknight.blog.51cto.com/2599480/1632808
[Python練習]對比兩個目錄裡面的檔案差異,並給出差異列表