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写的

相关推荐
Lumbrologist23 分钟前
【C++】零基础入门 · 第 3 节:条件判断(if、switch)
开发语言·c++·算法
布吉岛的石头24 分钟前
Java 程序员第 22 阶段:Function Call 工具调用实战,Java 封装大模型外部能力
java·人工智能·python
l1t29 分钟前
DeepSeek总结的使用实体-组件-系统和基于存在性处理进行Python编程简介
开发语言·python
曲幽35 分钟前
FastApiAdmin 后端接口开发好了,前端管理界面怎么调用与显示?
python·vue3·api·fastapi·web·ant design·view·menu·frontend
Lhan.zzZ41 分钟前
使用 ctx.lineDash 根治 QML Canvas 虚线残留问题(支持 Qt 5.12/5.14 等版本)
开发语言·qt
雨落在了我的手上42 分钟前
初识java(十一):继承
java·开发语言
xier_ran43 分钟前
【infra之路】从“三堵叹息之墙”到异构计算的狂飙
开发语言·c++·算法
rayyy91 小时前
神经网络模型的外推性验证
pytorch·python·深度学习
长乐呀1 小时前
数据集获取与整理
python
yaoxin5211231 小时前
417. 现代 Java IO 最佳实践 - 高效遍历、ZIP 处理与临时文件管理
java·开发语言·windows