Python 归档打包进度条

python 复制代码
#!/bin/python3

import os
import subprocess

from rich.progress import BarColumn, Progress, TimeRemainingColumn


def count_files_in_directory(directory):
    # Recursively count all files in the directory
    total_files = 0
    for root, dirs, files in os.walk(directory):
        total_files += len(files)
    return total_files


def tar_with_progress(source_dir, output_tar):
    total_files = count_files_in_directory(source_dir)

    if total_files == 0:
        print(f"No files found in directory: {source_dir}")
        return

    # Command to run tar and capture output
    tar_command = ["tar", "cf", output_tar, "-C", source_dir, "."]

    with Progress(
        "[progress.description]{task.description}",
        BarColumn(),
        "[progress.percentage]{task.percentage:>3.0f}%",
        TimeRemainingColumn(),
    ) as progress:
        task_id = progress.add_task(f"Archiving {source_dir}...", total=total_files)

        # Start the tar process
        process = subprocess.Popen(
            tar_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
        )

        files_processed = 0
        for root, dirs, files in os.walk(source_dir):
            for file in files:
                # Simulate processing of each file (tar doesn't provide per-file feedback)
                files_processed += 1
                progress.update(task_id, completed=files_processed)

        process.wait()

        # Check if the tar command completed successfully
        if process.returncode == 0:
            print(f"Archive created successfully: {output_tar}")
        else:
            print(f"Error creating archive: {output_tar}")


import sys

if __name__ == "__main__":
    source_directory = sys.argv[1]  # Replace with your source directory
    output_tarfile = sys.argv[2]  # Replace with your desired output tar file name
    tar_with_progress(source_directory, output_tarfile)

好像是AI写的

相关推荐
万邦科技Lafite21 分钟前
阿里巴巴拍立淘按图搜索商品API返回值实践:提升用户购物满意度的关键措施
开发语言·api·开放api·电商开放平台·京东开放平台
小小龙学IT1 小时前
Qt 6 跨平台开发完全指南:从信号槽机制到实际项目落地
开发语言·qt
leisoo80971 小时前
ig50数据落盘ClickHousevsTimescaleDBvsDuckDB实测对比 IG50免费开源股票数据API接口
开发语言·jvm·数据库·python·json
卷无止境1 小时前
FastAPI 的 Metadata 到底是什么,又牵动了哪些核心概念
后端·python
卷无止境1 小时前
FastAPI 调试实战,从断点到生产环境的排错心法
后端·python
敢敢のwings2 小时前
智元 GO-2 与 AgiBot-World 深度解读
开发语言·后端·golang
yaoxin5211232 小时前
503. Java 反射 - 编写 ServiceFactory 类
java·开发语言·python
一个帅气昵称啊3 小时前
.Net C# AI智能体开发-快速开始
开发语言·c#·.net
kyrie_sakura3 小时前
python学习笔记3 -- 流程控制语句结构
笔记·python·学习
冬夜戏雪3 小时前
agent的trace 和eval
开发语言·前端·javascript