class Error(Exception):
"""Base class for other exceptions"""
pass
class ValueIsNotInRange(Error): pass
def convertData(file):
try:
with open(file, 'r') as file:
data = file.readlines()
message = ""
for row in data:
for code in row.split(' '):
if int(code.rstrip('\n')) in range(128):
message += chr(int(code)) if not '\n' in code else chr(int(code)) + '\n'
else:
raise ValueIsNotInRange # return 3
with open("message.txt", 'w') as file:
file.write(message)
except IOError:
return 1
except ValueError:
return 2
except ValueIsNotInRange:
return 3
return 0
print(convertData("data.txt")) # return 0
print(convertData("liczby.txt")) # return 1
print(convertData("data2.txt")) # return 2
print(convertData("data3.txt")) # return 3
77 79 78 84 89
80 89 84 72 79 78
77 79 78 T Y
80 89 84 72 79 78
77 79 78 127 0 -1 128
80 89 84 72 79 78