• Python 每天定时加密压缩指定文件夹


    一个简单的"每天定时加密压缩指定文件夹"的小工具

    import shutil
    import subprocess
    import os
    import schedule
    import time
    import logging
    from pathlib import Path
     
    def encrypt_folder_with_winrar(folder_path, password):
        # WinRAR命令
        winrar_command = r'C:\Program Files \WinRAR\WinRAR.exe'
     
        # 获取当前日期
        current_date = time.strftime("%Y-%m-%d", time.localtime())
     
        # 获取桌面BD文件夹路径
        desktop_path = str(Path.home() / "Desktop")
        bd_folder_path = os.path.join(desktop_path, "DB")
     
        # 创建DB文件夹(如果不存在)
        if not os.path.exists(bd_folder_path):
            os.mkdir(bd_folder_path)
     
        # 压缩包文件名
        compressed_filename = f'压缩包-{current_date}.rar'
     
        # 压缩包路径
        compressed_filepath = os.path.join(bd_folder_path, compressed_filename)
     
        # 加密命令
        encryption_command = [winrar_command, 'a', '-r', '-hp' + password, compressed_filepath, folder_path]
     
        # 执行加密命令
        subprocess.run(encryption_command)
     
        return compressed_filepath
     
    def encrypt_and_delete_folder(folder_path, password):
        try:
            # 压缩并加密文件夹
            compressed_file = encrypt_folder_with_winrar(folder_path, password)
     
            # 等待一段时间,确保压缩文件完成
            time.sleep(10)
     
            # 删除源文件夹中的文件
            for root, dirs, files in os.walk(folder_path):
                for file in files:
                    os.remove(os.path.join(root, file))
     
            logger.info("加密打包操作成功")
        except Exception as e:
            logger.error(f"加密打包操作失败:{e}")
     
            # 出现错误时,删除已创建的压缩文件
            if os.path.exists(compressed_file):
                os.remove(compressed_file)
                logger.info("压缩文件已删除.")
     
    # 创建日志记录器
    logger = logging.getLogger("encrypt_and_delete")
    logger.setLevel(logging.INFO)
     
    # 创建文件处理器
    log_file = "encrypt_and_delete.log"
    file_handler = logging.FileHandler(log_file)
    file_handler.setLevel(logging.INFO)
     
    # 创建控制台处理器
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.INFO)
     
    # 创建日志格式
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    file_handler.setFormatter(formatter)
    console_handler.setFormatter(formatter)
     
    # 添加处理器到记录器
    logger.addHandler(file_handler)
    logger.addHandler(console_handler)
     
    # 文件夹路径
    folder_path = r'D:\BACK'
     
    # 密码
    password = '123456'
     
    # 每天定时执行加密打包操作
    schedule.every().day.at("12:20").do(encrypt_and_delete_folder, folder_path=folder_path, password=password)
     
    # 循环执行任务,并写入日志
    while True:
        schedule.run_pending()
        time.sleep(60)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
  • 相关阅读:
    用Vue编写一个简单的仿Explorer文件管理器
    【优化调度】基于改进遗传算法的公交车调度排班优化的研究与实现(Matlab代码实现)
    Redis的分布式锁
    路由 —— 源站路由 + 策略路由
    c 的网络I/O库总结(libevent,libuv,libev,libeio)
    K8S集群中Pod资源处于Error状态排查思路
    vue使用高德地图-进行显示地图和查询天气
    图神经网络通用框架 —— MPNN消息传递神经网络
    经典算法之插入排序(InsertionSort)
    Java中循环删除list报错解析
  • 原文地址:https://blog.csdn.net/qq_29130719/article/details/132906716