簡體   English   中英

使用tqdm時如何抑制文件錯誤的output?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM