#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "月影鹏鹏"
__date__ ="$2011-2-16 17:39:40$"

'''
QQ:1356281767
email: jk@aiwaly.com
web: http://www.aiwaly.com

'''
import os,time ,sys ,shutil
#获得文件后缀                
def file_extension(path):
    return os.path.splitext(path)[1]

import gzip

BufSize = 1024*8 

#压缩
def gZipFile(src, dst):
    fin = open(src, 'rb')
    fout = gzip.open(dst, 'wb')

    in2out(fin, fout)
#解压      
def gunZipFile(gzFile, dst):
    fin = gzip.open(gzFile, 'rb')
    fout = open(dst, 'wb')

    in2out(fin, fout)

def in2out(fin, fout):
    while True:
        buf = fin.read(BufSize)
        if len(buf) < 1:
            break
        fout.write(buf)

    fin.close()
    fout.close()


import hashlib
def CalcSha1(filepath):
        with open(filepath,'rb') as f:
                sha1obj = hashlib.sha1()
                sha1obj.update(f.read())
                hash = sha1obj.hexdigest()
                print(hash)
                return hash





def FilenameAddSHA1( filename): 

        print(filename)
        hashtag=CalcSha1(filename)
        extension_name =file_extension (os.path.join(filename))
        fname,ext = os.path.splitext(filename)        
        print(fname + "-SHA1-" + hashtag +  extension_name)
        shutil.move(filename,fname + "-SHA1-" + hashtag +  extension_name)

def FilenameAddSHA1andGzip( filename): 
        print(filename)
        hashtag=CalcSha1(filename)
        extension_name =file_extension (os.path.join(filename))
        fname,ext = os.path.splitext(filename)        
        print(fname + "-SHA1-" + hashtag +  extension_name)

        gZipFile(filename,   fname + "-SHA1-" + hashtag +  extension_name + ".gz")       
        os.remove(filename)


def file_path(path):
    for fpathe,dirs,fs in os.walk(path):
           for f in fs :
                 print( os.path.join(fpathe,f))
                 extension_name =file_extension (os.path.join(fpathe,f))

                 if '.SHA1-' in  os.path.join(fpathe,f):
                     continue                      
                 if  extension_name.lower()  == ".jpg" or extension_name   == ".CR2"  or extension_name   == ".DNG":
                     FilenameAddSHA1(os.path.join(fpathe,f))
                 #if  extension_name   == ".CR2"  or extension_name   == ".DNG" :
                 #    FilenameAddSHA1andGzip(os.path.join(fpathe,f))


#print(os.path.basename(os.path.realpath(__file__)))
#path=sys.argv[1]
path="D:/awshare/myphoto/Rp2016"
print(path)

file_path(path)
文档更新时间: 2019-06-14 13:38   作者:月影鹏鹏