-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
29 lines (23 loc) · 696 Bytes
/
errors.py
File metadata and controls
29 lines (23 loc) · 696 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
28
29
#!/usr/bin/env python
import sys
import sys
import time
import logging
# EAFP - Easy to Ask Forgiveness than permission
# (É mais fácil pedir perdão do que permissão)
log = logging.Logger("errors")
def try_open_a_file(filepath, retry=1):
"""Tries to open a file, if error, retries n times"""
for attempt in range(1, retry + 1):
try:
return open(filepath).readlines()
except FileNotFoundError as e:
log.error("ERRO: %s", str(e))
time.sleep(2)
else:
print("Sucesso!")
finally:
print("Execute isso sempre!")
return []
for line in try_open_a_file("names.txt", retry=5):
print(line)