'tqdm' object has no attribute 'last_print_t' 错误解决方法:改用其他进度库
使用 'tqdm' 库时,有时会遇到 'AttributeError: 'tqdm' object has no attribute 'last_print_t'' 错误,这通常是由于 'tqdm' 库版本过高导致的。tqdm==4.65.0 或者 tqdm==4.60.0 都可能出现此问题。
为了解决此问题,可以考虑使用其他进度库,比如 'progress' 或者 'alive_progress'。
以下是改用 'progress' 库后的代码:
import os
import subprocess
from progress.bar import Bar
def pack_files(source_dir, 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
# 计算文件总数
total_files = sum([len(files) for root, dirs, files in os.walk(source_dir)])
# 外层进度条(总进度)
with Bar('压缩进度', max=total_files, suffix='%(percent)d%% [%(elapsed_td)s / %(eta_td)s]') as pbar:
for root, dirs, files in os.walk(source_dir):
for file in files:
# 获取需要压缩的文件路径
file_path = os.path.join(root, file)
# 获取文件名和文件后缀
file_name, file_ext = os.path.splitext(file)
# 拼接输出文件路径(去掉文件后缀)
output_file = os.path.join(output_dir, f'{file_name}.gz')
if os.path.isdir(file_path):
# 处理文件夹,使用tar命令打包并压缩成tar.gz格式
tar_file = os.path.join(output_dir, f'{file_name}.tar.gz')
subprocess.run(['tar', '-czf', tar_file, file_path])
print(f'已将文件夹{file_path}打包压缩到{tar_file}')
else:
# 处理文件,使用pigz进行压缩
subprocess.run(['pigz', '-c', file_path],
stdout=open(output_file, 'wb'))
print(f'已将文件{file_path}压缩到{output_file}')
# 更新外层进度条
pbar.next()
# 路径
source_dir = '/home/115/up'
output_dir = '/home/115/app'
pack_files(source_dir, output_dir)
'progress' 库的使用方法与 'tqdm' 库类似,您可以根据需要选择不同的进度库。
原文地址: https://www.cveoy.top/t/topic/nDeX 著作权归作者所有。请勿转载和采集!