File33. Дан файл целых чисел. Удалить из него все элементы с четными номерами.
Решение на Python 3:
# -*- coding: utf-8 -*-
import random
import sys
import os
def GenerateNumbers(fname):
N = random.randint(0,20)
print("N = ",N)
try:
f = open(fname, "w")
for i in range(0,N):
#x = random.randrange(1,10)
x = i + 1
line = str(x)+"\n"
f.write(line)
f.close()
except:
print("Error: ", sys.exc_info()[0])
f_input = "file33_in.txt"
f_output = "file33_out.txt"
GenerateNumbers(f_input)
print("Read from:",f_input)
print("Write to:",f_output)
try:
k = 1
with open(f_input, 'r') as f_in, open(f_output, 'w') as f_out:
for line in f_in:
if k == 1:
f_out.write(line)
k *= -1
except:
print("Error: ", sys.exc_info())