[英]How can I suppress the output of file errors when using tqdm?
我正在加载一堆文件,并希望使用 tqdm 显示相应的进度条。
for file_path in tqdm(file_paths, position=0, desc='files loaded'):
if is_binary(file_path):
continue
try:
with open(file_path, 'r', encoding='utf8', errors='ignore') as input_file:
file_content = input_file.read()
processing_queue.put(file_content)
except FileNotFoundError as e:
main_logger.error(f'Encountered exception while opening {file_path}: {e}')
即使我正在处理异常找不到的文件,我仍然会在控制台上打印出错误消息,这些消息会干扰 tqdm 的 output:
files loaded: 99%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 257398/260429 [6:17:16<07:16, 6.95it/s]
[Errno 2] No such file or directory: '\\\\server\\path\\to\\file'██████ | 112570/260429 [6:17:11<7:05:21, 5.79it/s]
files loaded: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 260429/260429 [6:21:29<00:00, 11.38it/s]
为什么这些消息仍然打印到控制台,可以做些什么来抑制它们?
好的,我先在单独的 try/except 块中打开文件来修复它!
for file_path in tqdm(file_paths, position=0, desc='files loaded'):
try:
open(file_path).close()
except EnvironmentError as e:
main_logger.error(f'Encountered exception while opening {file_path}: {e}')
continue
if is_binary(file_path):
continue
with open(file_path, 'r', encoding='utf8', errors='ignore') as input_file:
file_content = input_file.read()
processing_queue.put(file_content)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.