File15. Дан файл вещественных чисел. Найти сумму его элементов с четными номерами.

Решение на Python 3:

def EmptyFile(fname):
open(fname, 'w').close()

def Write2File(fname,lst):
try:
f = open(fname, "a")
try:
for line in lst:
f.write(line)
finally:
f.close()
except IOError:
print('Write error: ',fname)

f_source = "file15_source.txt"
f_even = "file15_even.txt"
f_odd = "file15_odd.txt"
EmptyFile(f_even)
EmptyFile(f_odd)

try:
with open(f_source,'r') as f:
lines = f.readlines()
#print(lines)
l_even = lines[::2]
l_odd = lines[1::2]
print(l_even)
print(l_odd)
Write2File(f_even,l_even)
Write2File(f_odd,l_odd)
float_even = list(map(float, l_even))
float_odd = list(map(float, l_odd))
print("sum of evens:",sum(float_even))
print("sum of odds:",sum(float_odd))
except IOError:
print('Open error: ',f_source)