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

相关推荐
赵文宇(温玉)10 分钟前
构建内网离线的“github.com“,完美解决内网Go开发依赖
开发语言·golang·github
qq74223498414 分钟前
Python操作数据库之pyodbc
开发语言·数据库·python
Joker1008515 分钟前
仓颉自定义序列化:从原理到高性能多协议实现
开发语言
Adellle19 分钟前
2.单例模式
java·开发语言·单例模式
散峰而望28 分钟前
C++入门(一)(算法竞赛)
c语言·开发语言·c++·编辑器·github
C_Liu_35 分钟前
13.C++:继承
开发语言·c++
张人玉36 分钟前
c#串口读写威盟士五插针
开发语言·c#·通讯
路长冬44 分钟前
matlab与数字信号处理的不定期更新
开发语言·matlab·信号处理
2401_841495641 小时前
【自然语言处理】轻量版生成式语言模型GPT
人工智能·python·gpt·深度学习·语言模型·自然语言处理·transformer
卡卡酷卡BUG1 小时前
Java 后端面试干货:四大核心模块高频考点深度解析
java·开发语言·面试