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

相关推荐
多敲代码防脱发37 分钟前
为何引入Spring-cloud以及远程调用(RestTemplate)
java·开发语言
第二只羽毛41 分钟前
主题爬虫采集主题新闻信息
大数据·爬虫·python·网络爬虫
plmm烟酒僧43 分钟前
TensorRT 推理 YOLO Demo 分享 (Python)
开发语言·python·yolo·tensorrt·runtime·推理
天才测试猿1 小时前
Postman中变量的使用详解
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
sailing-data1 小时前
【SE】接口标准化
java·开发语言
无名3871 小时前
RTPEngine 官方自带的 perl 测试程序
开发语言·perl·通信
帕巴啦1 小时前
Arcgis计算面要素的面积、周长、宽度、长度及最大直径
python·arcgis
fruge1 小时前
接口 Mock 工具对比:Mock.js、Easy Mock、Apifox 的使用场景与配置
开发语言·javascript·ecmascript
AI小云1 小时前
【数据操作与可视化】Matplotlib绘图-生成其他图表类型
开发语言·python·matplotlib
MediaTea1 小时前
Python 第三方库:plotnine(类 ggplot 的 Python 数据可视化库)
开发语言·python·信息可视化