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

相关推荐
Jose_lz1 小时前
C#开发学习杂笔(更新中)
开发语言·学习·c#
一位代码1 小时前
python | requests爬虫如何正确获取网页编码?
开发语言·爬虫·python
可触的未来,发芽的智生1 小时前
新奇特:神经网络速比器,小镇债务清零的算法奇缘
javascript·人工智能·python
看到我,请让我去学习1 小时前
Qt 控件 QSS 样式大全(通用属性篇)
开发语言·c++·qt
mortimer1 小时前
还在被 Windows 路径的大小写和正反斜杠坑?是时候让 pathlib 拯救你的代码了!
人工智能·python
筱砚.1 小时前
【STL——vector容器】
开发语言·c++
lly2024061 小时前
数据访问对象模式(Data Access Object Pattern)
开发语言
std860212 小时前
Rust 与 Python – 这是未来的语言吗?
开发语言·python·rust
鄃鳕2 小时前
Flask【python】
后端·python·flask
2503_930123932 小时前
Kubernetes (六)调度策略详解:从节点匹配到Pod调度全流程
java·开发语言