Python移动文件并做 md5 校验

这篇文章是 另外一个博客的延续 ,这个使用os.listdir写成的
# encoding: utf-8
import os
import io
import sys
import shutil
import hashlib
import re


def Usage():
    print("Usage:\n\t%s src_dir dst_dir" % (os.path.basename(sys.argv[0])))
    sys.exit(4)


def checksrcdir(src_dir):
    if not os.path.exists(src_dir):
        print("源文件夹: %s 不存在" % src_dir)
        sys.exit(3)
    return False


def checkdstdir(dst_dir):
    if not os.path.exists(dst_dir):
        os.makedirs(dst_dir)
    return True


def computmd5(filename, readsize):
    hs = hashlib.md5()
    with open(filename, mode='rb') as bigfile:
        content = bigfile.read(readsize)
        if content:
            hs.update(content)
    return hs.hexdigest()


def checkfilesize(filename):
    filesize = os.path.getsize(filename)
    return filesize


def write_log_to_file(filename, messages):
    """
    :param filename:
    :param messages:
    """
    with open(filename, mode='a+', encoding='utf-8') as rg:
        rg.writelines(messages)
    rg.close()


def truncatefile(filename):
    with open(filename, mode='w+', encoding='utf-8') as rg:
        rg.truncate()
    rg.close()


def copyfile(srcfile, dstfile):
    shutil.copy2(src=srcfile, dst=dstfile)


def countnumber(dirs,filename):
    count = 0
    for _ in os.listdir(dirs):
        # print(file)
        count += 1
    msg = "%s 目录下共有 %d 个文件\n" % (dirs, count)
    write_log_to_file(filename,messages=msg)


def scandirictory(dirs):

    count = 0
    for files_or_dirictories in os.listdir(dirs):
        count += 1
        src_full_file_path = os.path.join(dirs, files_or_dirictories)

        if os.path.isfile(src_full_file_path):

            size = checkfilesize(src_full_file_path)
            if size < 104857600:
                # print(src_full_file_path)
                dst_full_file_path = src_full_file_path.replace(src_dir,dst_dir)
                srcMd5Value = computmd5(filename=src_full_file_path,readsize=-1)
                # print(dst_full_file_path)
                if os.path.exists(dst_full_file_path):
                    dstMd5Value = computmd5(filename=dst_full_file_path,readsize=-1)

                    if not srcMd5Value == dstMd5Value:
                        # print(dst_full_file_path)
                        msg = "%s --> %s md5不匹配,立即替换 %s。\n\t源文件md5为%s,终文件md5为:%s\n" % (src_full_file_path,dst_full_file_path,dst_full_file_path,
                                                                                 srcMd5Value,dstMd5Value)
                        copyfile(srcfile=src_full_file_path,dstfile=dst_dir)
                        write_log_to_file(filename='oslistdir_error.log',messages=msg)
                    else:
                        msg = "%s 已存在 %s \n" % (src_full_file_path,dst_full_file_path)
                        write_log_to_file(filename='oslistdir_exist.log',messages=msg)
                else:
                    copyfile(srcfile=src_full_file_path,dstfile=dst_full_file_path)
                    dstMd5Value = computmd5(filename=dst_full_file_path, readsize=-1)
                    if srcMd5Value == dstMd5Value:
                        msg = "%s 复制到 %s\n\t源文件md5:%s\t终文件md5:%s\n" % (src_full_file_path,dst_full_file_path,srcMd5Value,dstMd5Value)
                        write_log_to_file(filename='oslistdir_copied.log',messages=msg)
            else:
                dst_full_file_path = src_full_file_path.replace(src_dir, dst_dir)
                srcMd5Value = computmd5(filename=src_full_file_path, readsize=104857600)
                # print(dst_full_file_path)
                if os.path.exists(dst_full_file_path):
                    dstMd5Value = computmd5(filename=dst_full_file_path, readsize=104857600)

                    if not srcMd5Value == dstMd5Value:
                        # print(dst_full_file_path)
                        msg = "%s --> %s md5不匹配,立即替换 %s。\n\t源文件md5为%s,终文件md5为:%s\n" % (
                        src_full_file_path, dst_full_file_path, dst_full_file_path,
                        srcMd5Value, dstMd5Value)
                        copyfile(srcfile=src_full_file_path, dstfile=dst_dir)
                        write_log_to_file(filename='oslistdir_error.log', messages=msg)
                    else:
                        msg = "%s 已存在 %s \n" % (src_full_file_path, dst_full_file_path)
                        write_log_to_file(filename='oslistdir_exist.log', messages=msg)
                else:
                    copyfile(srcfile=src_full_file_path, dstfile=dst_full_file_path)
                    dstMd5Value = computmd5(filename=dst_full_file_path, readsize=-1)
                    if srcMd5Value == dstMd5Value:
                        msg = "%s 复制到 %s\n\t源文件md5:%s\t终文件md5:%s\n" % (
                        src_full_file_path, dst_full_file_path, srcMd5Value, dstMd5Value)
                        write_log_to_file(filename='oslistdir_copied.log', messages=msg)

        if os.path.isdir(src_full_file_path):

            # print(files_or_dirictories)
            ds = src_full_file_path.replace(src_dir+'\\',dst_dir+'\\')

            if not os.path.exists(ds):
                os.makedirs(ds)
            scandirictory(src_full_file_path)
            countnumber(dirs=ds,filename="dist_oslistdir_number.log")
    msg = "%s 有 %d 个文件\n" % (dirs, count)
    write_log_to_file(filename='source_oslistdir_number.log',messages=msg)




if __name__ == '__main__':
    # if sys.argv.__len__() < 2:
    #     Usage()
    # checksrcdir(sys.argv[1])
    # checkdstdir(sys.argv[2])
    # src_dir='C:\\Users\\qixiao\\Downloads'
    # src_dir = "E:\Java\jdk-13.0.1"
    src_dir = "F:\VirtualMachines\openmanager"
    dst_dir = 'F:\Downloads'
    checksrcdir(src_dir=src_dir)
    checkdstdir(dst_dir=dst_dir)
    # countnumber(src_dir)
    scandirictory(src_dir)
    countnumber(dirs=dst_dir,filename="dist_oslistdir_number.log")
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值