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

相关推荐
望获linux5 分钟前
【Linux基础知识系列】第六十四篇 - 了解Linux的硬件架构
linux·运维·服务器·开发语言·数据库·操作系统·嵌入式软件
空中湖6 分钟前
PyTorch武侠演义 第一卷:初入江湖 第7章:矿洞中的计算禁制
人工智能·pytorch·python
Emma歌小白20 分钟前
**大数据量(几千万行)划分价格区间(价格段)
python
马哥python说44 分钟前
【效率软件】抖音转换工具:主页链接和抖音号一键批量互转
爬虫·python
江山如画,佳人北望1 小时前
pytorch常用函数
人工智能·pytorch·python
这里有鱼汤1 小时前
首个开源金融平台,一站式数据终端 + AI 代理,量化研究者的利器,速来白嫖
后端·python
这里有鱼汤1 小时前
Python量化实战:如何用Python实现查找相似K线,附源码,建议收藏
后端·python
1candobetter1 小时前
JAVA后端开发——用 Spring Boot 实现定时任务
java·开发语言·spring boot
小阿鑫1 小时前
使用 Kiro AI IDE 3小时实现全栈应用Admin系统
前端·后端·python·admin·kiro·next admin·fastapi admin
Blossom.1181 小时前
基于深度学习的医学图像分析:使用YOLOv5实现细胞检测
人工智能·python·深度学习·yolo·机器学习·分类·迁移学习