-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
27 lines (23 loc) · 783 Bytes
/
errors.py
File metadata and controls
27 lines (23 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python3
import os
import sys
import time
import logging
log = logging.Logger('errors')
# EAFP - Easy to Ask Forgiveness than Permission
# (É mais fácil pedir perdão do que permissão)
def try_to_open_a_file(filepath, retry=1) -> list:
"""Tries to open a file, if error, retries n times."""
for attempt in range(1, retry + 1):
try:
return open(filepath).readlines() # FileNotFoundError
except FileNotFoundError as e:
log.error('ERRO: %s', e)
time.sleep(2)
else:
print("Sucesso") #só executa se não executar o except
finally:
print("execute isso sempre") # executa sempre
return []
for line in try_to_open_a_file('names.txt', retry=5):
print(line)