[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里面没有的文件
相关文章推荐
- python difflib模块实现两个文件差异对比,并输出html格式。
- Python自动化运维笔记(五):使用filecmp模块实现文件以及文件目录差异对比
- Python 获取目录下的文件列表与内容
- Python使用difflib对比两个文件操作实例
- Python 通过difflib模块实现文件内容差异对比
- C#访问共享目录 得到共享目录文件列表和修改里面的文件
- 文件内容差异对比-python
- Python使用difflib对比两个文件操作实例
- python 获取文件列表(或是目录例表)
- 两个文件内容差异对比,
- python目录和文件的列表清单
- Python利用列表深度遍历目录、复制文件
- python比较两个文件的差异
- Python自动化运维_文件内容差异对比分析
- # 输出目录下的文件 os模块 分类: python 小练习 2013-05-13 18:01 205人阅读 评论(0) 收藏
- python小练习6:合并两个列表,相同的元素不要
- python比较两个文件的差异
- eclipse里面对比两个java文件
- #小练习 os.remove() 删除目录文件 分类: python 小练习 2013-05-21 15:49 2774人阅读 评论(0) 收藏
- Python使用difflib对比两个文件操作实例