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

相关推荐
gCode Teacher 格码致知8 小时前
Python基础教学:如何拼接字符串?-由Deepseek产生
python
还债大湿兄8 小时前
阿里通义千问调用图像大模型生成轮动漫风格 python调用
开发语言·前端·python
blank@l8 小时前
python测开小工具--日志查询分析工具
python·python接口自动化测试基础·python测试开发·日志查询分析·日志分析统计查询·软件测试工具·argparse模块
hu_nil8 小时前
LLMOps-第十三周
python·vllm
空影星8 小时前
轻量日记神器RedNotebook,高效记录每一天
python·数据挖掘·数据分析·音视频
鸭子程序员8 小时前
c++ 算法
开发语言·c++·算法
搬砖ing换来金砖8 小时前
Python入门-Task02
开发语言·python
雨中散步撒哈拉9 小时前
17、做中学 | 初三下期 Golang文件操作
开发语言·后端·golang
databook9 小时前
告别盲人摸象,数据分析的抽样方法总结
后端·python·数据分析
CoderYanger9 小时前
C.滑动窗口——1423. 可获得的最大点数
java·开发语言·算法·leetcode·1024程序员节