python生成哈希目录

c++代码获取一个字符串的md5值并输出

#include "md5.h"
#include <string>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

extern "C"
{
    void getStrMd5(char* filePath)
    {
        MD5 md5(filePath);
        std::string str = md5.md5();

        printf("%s", str.c_str());
    }
}


生成动态库,供python调用
g++ md5.cpp getStringMd5.cpp -fPIC -shared -o getHash.so

获取字符串的md5值:getHashValue.py

#!/usr/bin/python

from ctypes import *
import os 
import sys

target = (sys.argv)[1]

libtest = cdll.LoadLibrary(os.getcwd() + '/getHash.so') 
print libtest.getStrMd5(target)

处理得到哈希目录脚本:process.py
32位md5值,前4位用作目录名, 后28位用作文件名

#!/usr/bin/python

import sys
import os
import os.path
import shutil
import commands
srcPath = os.path.join((sys.path)[0], "res")
desPath = os.path.join((sys.path)[0], "HashRes")

if os.path.exists(desPath):
    shutil.rmtree(desPath)
os.mkdir(desPath)

def process(path):
    for parent, dirnames, filenames in os.walk(path):
        for file in filenames :
            if file != ".DS_Store" :
                fullPath = parent + "/" + file
                splitPath = fullPath[len(path)+1:]
                command = "python getHashValue.py " + splitPath
                f = os.popen(command)
                data = f.readlines()[0][0:31]

                fileNewPath = desPath + "/" + data[0:4]
                if not os.path.exists(fileNewPath):
                    os.mkdir(fileNewPath)
                shutil.copyfile(parent+"/"+file, fileNewPath+"/"+data[4:32])


process(os.path.join((sys.path)[0], "res"))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值