问题描述
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的输出:
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)