您的位置:首页 > 编程语言 > Python开发

[Python练习]对比两个目录里面的文件差异,并给出差异列表

2015-04-15 09:38 756 查看
#!/usr/bin/env python
import os
import sys

def 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_Files

item_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_List

add_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_File

del_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里面没有的文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息