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

相关推荐
武昌库里写JAVA2 分钟前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计
ZSYP-S37 分钟前
Day 15:Spring 框架基础
java·开发语言·数据结构·后端·spring
yuanbenshidiaos40 分钟前
c++------------------函数
开发语言·c++
程序员_三木1 小时前
Three.js入门-Raycaster鼠标拾取详解与应用
开发语言·javascript·计算机外设·webgl·three.js
是小崔啊1 小时前
开源轮子 - EasyExcel01(核心api)
java·开发语言·开源·excel·阿里巴巴
tianmu_sama1 小时前
[Effective C++]条款38-39 复合和private继承
开发语言·c++
黄公子学安全1 小时前
Java的基础概念(一)
java·开发语言·python
liwulin05061 小时前
【JAVA】Tesseract-OCR截图屏幕指定区域识别0.4.2
java·开发语言·ocr
jackiendsc1 小时前
Java的垃圾回收机制介绍、工作原理、算法及分析调优
java·开发语言·算法
Oneforlove_twoforjob1 小时前
【Java基础面试题027】Java的StringBuilder是怎么实现的?
java·开发语言