将下面python3代码中的压缩进度条样式改为绿色#号import osimport subprocessfrom tqdm import tqdmdef pack_filessource_dir output_dir 使用pigz单独打包自定义目录下每个文件并将打包后的文件输出到output_dir中。 param source_dir 需要打包的目录 param ou
将tqdm中的desc参数改为'压缩进度',并添加参数bar_format='#{bar}',代码如下:
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='压缩进度', bar_format='#{bar}'):
# 获取需要压缩的文件路径
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)
``
原文地址: http://www.cveoy.top/t/topic/c4lL 著作权归作者所有。请勿转载和采集!