可以使用tqdm库来增加显示压缩进度的功能。具体实现如下所示:

import os import subprocess from tqdm import tqdm

def pack_files(source_dir, output_dir): ''' 使用pigz单独打包自定义目录下每个文件,并将打包后的文件输出到output_dir中。 :param source_dir: 需要打包的目录 :param output_dir: 打包后文件输出目录 ''' # 检查源目录和输出目录是否存在 if not os.path.exists(source_dir) or not os.path.isdir(source_dir): print(f'源目录{source_dir}不存在或不是一个目录') return

if not os.path.exists(output_dir) or not os.path.isdir(output_dir):
    print(f'输出目录{output_dir}不存在或不是一个目录')
    return

for root, dirs, files in os.walk(source_dir):
    for file in tqdm(files, desc='压缩进度'):
        # 获取需要压缩的文件路径
        file_path = os.path.join(root, file)
        
        # 拼接输出文件路径
        output_file = os.path.join(output_dir, f'{file}.gz')
        
        # 使用pigz进行压缩
        subprocess.run(['pigz', '-c', file_path], stdout=open(output_file, 'wb'))
        
        print(f'已将文件{file_path}压缩到{output_file}')

测试代码

source_dir = '/home/115/up' output_dir = '/home/115/app' pack_files(source_dir, output_dir)

在上面的代码中,我们使用tqdm库来增加了一个进度条,使得用户可以清晰地看到压缩的进度。在for循环中,我们将tqdm函数应用于files列表,同时为进度条添加了一个描述信息“压缩进度”。这样,在压缩的过程中,用户就可以看到一个美观的进度条,并且知道压缩的进度

为下面python3代码增加显示压缩进度import osimport subprocessdef pack_filessource_dir output_dir 使用pigz单独打包自定义目录下每个文件并将打包后的文件输出到output_dir中。 param source_dir 需要打包的目录 param output_dir 打包后文件输出目录 #

原文地址: http://www.cveoy.top/t/topic/c4ki 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录