码迷,mamicode.com
首页 > 系统相关 > 详细

Linux 两个目录浅对比拷贝

时间:2016-11-04 01:54:55      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:imp   bin   compare   start   art   correct   com   tor   parameter   

对比两个目录内容,然后拷贝!

#!/usr/bin/python
# -*-coding:utf-8 -*-

import os
import sys
import shutil


def get_dir_content(dir):
dir_list = []
dir_files = os.listdir(dir)
for dir_file in dir_files:
if not dir_file.startswith(‘.‘):
dir_list.append(dir_file)
return dir_list


def compare_dir(dir1, dir2):
parent_dirs = get_dir_content(dir1)
child_dirs = get_dir_content(dir2)
for child_dir in child_dirs:
if os.path.isfile(child_dir):
os.system("cp %s/%s %s" % (dir2, child_dir, dir1))
elif child_dir in parent_dirs:
os.system("cp -r %s/%s/* %s/%s/" % (dir2, child_dir, dir1, child_dir))
else:
os.system("cp -r %s/%s %s/" % (dir2, child_dir, dir1))
return True


def help(param="-h"):
if param in ["--help", "-h"]:
print ‘‘‘
dir_compare.py [parent_dir] [child_dir]
parent_dir is move the following directory
child_dir is move the previous directory
dir_compare.py [-h or --help]
get help
‘‘‘
sys.exit()
else:
print "Re-enter the parameter incorrectly"
sys.exit()


if __name__ == ‘__main__‘:
if len(sys.argv) == 1:
help()
elif len(sys.argv) != 3:
help(sys.argv[1])
else:
compare_dir(sys.argv[1], sys.argv[2])
shutil.rmtree(sys.argv[2])

Linux 两个目录浅对比拷贝

标签:imp   bin   compare   start   art   correct   com   tor   parameter   

原文地址:http://www.cnblogs.com/wangpengf/p/6028717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!