pyton练习,对两个文件夹子下文件名进行比较

本文介绍了一个使用Python编写的脚本,用于比较两个文件夹下的文件数量,并逐步扩展到比较文件名和内容。通过Git获取不同版本的代码,将每个版本的源代码检出并进行对比,以便找出最接近的版本。
摘要由CSDN通过智能技术生成

新代码的引擎有问题,引擎有若干版本源代码,不知道哪个版本是正确的。写个python程序,从git版本管理上每个版本取一次,自动比较,直到找到最相近的为止。

练习雏形

在/etc/profile 尾部添加我的shell脚本路径。所有又用的脚本都放到该路径下,以后调用这些脚本的时候就像调用系统命令一下,不用再输入路径了。比如我的脚本路径是/home/wulong/program/myshell。在/etc/profile尾部添加路径

#todo: wulong add my shell
myshell_path=/home/wulong/program/myshell
export PATH=$PATH:$myshell_path
export myshell_path
#endif


compare_folder.sh和compare_folder.py文件都放在myshell路径下

比如要击比较 ./src  与 ./old_src 。初版只比较文件数目。后面再添加 “比较文件名"--->比较文件内容0---->合并文件内容

调用方法如下 

$compare_folder.sh  ./src  ./old_src


compare_folder.sh内容如下

#!/bin/bash

if [ $# -eq 2 ] 
then
    parent_path=`pwd`
    one_path=`cd $parent_path ; cd $1 ; pwd`
    two_path=`cd $parent_path ; cd $2 ; pwd`
    
    
    sh_whole_path=`which compare_folder.sh` 
    sh_path=`dirname $sh_whole_path`
    cd $sh_path  #sh 与py在同一个文件夹下,文件路径要加到系统path中

    python compare_folder.py  $one_path $two_path 
    cd $parent_path
else
    echo "need two params"
fi

compare_folder.py内容如下

#!/usr/bin/python

import sys
import os

def get_file_list(path):
    f_l = []

    
    
    if os.path.isdir(path):
        for tmp in os.listdir(path):
            tmp = path + "/" + tmp
            if os.path.isdir(tmp):
                ret_list = get_file_list(tmp)
                f_l.extend(ret_list)
#                for tmp in ret_list:
#                    f_l.append(tmp)
            else:
                f_l.append(tmp)
                
    else:
        f_1.append(path)
        
    return f_l


def filter_path_name_list(path_list, parent_path):
    new_list = []
    if len(path_list):
        for tmp in path_list:
            tmp = tmp.replace(parent_path,"")
            new_list.append(tmp)
    return new_list


            
andengine_path=""
baofen_path=""

if len(sys.argv) == 3:
    andengine_path = sys.argv[1]
    baofen_path = sys.argv[2]

print len(sys.argv),", ",sys.argv[1],", ",sys.argv[2]
print andengine_path
print baofen_path

if os.path.isdir(andengine_path) and os.path.isdir(baofen_path):
    andengine_file_list = get_file_list(andengine_path)
    baofen_file_list = get_file_list(baofen_path)

    andeng
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值