File32. Дан файл целых чисел, содержащий четное количество элементов. Удалить из данного файла первую половину элементов.

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

# -*- coding: utf-8 -*-
import random
import sys
import os

def GenerateNumbers(fname):
N = random.randint(0,20)
N *= 2
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])

def CountFile(fname):
#count number of rows in given text-file
N = 0
try:
with open(fname,'r') as f:
for line in f:
N += 1
#print(N,":",line)
except IOError:
print("Open error:",fname)
return -1
finally:
return N

f_input = "file32_in.txt"
f_output = "file32_out.txt"
GenerateNumbers(f_input)
print("Read from:",f_input)
print("Write to:",f_output)

try:
N = CountFile(f_input)
print("N:",N)
N = N // 2
print("N:",N)

with open(f_input, 'r') as f_in, open(f_output, 'w') as f_out:
for i in range(N):
line = f_in.readline()
for i in range(N):
line = f_in.readline()
f_out.write(line)

except:
print("Error: ", sys.exc_info())