File36. Дан файл целых чисел. Удвоить его размер, записав в конец файла все его исходные элементы (в том же порядке).
Решение на Python 3:
# -*- coding: utf-8 -*-
import random
import sys
import os
def GenerateNumbers(fname):
N = random.randint(1,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 = "file36_in.txt"
f_output = "file36_out.txt"
GenerateNumbers(f_input)
print("Read from:",f_input)
print("Write to:",f_output)
try:
with open(f_input, 'r') as f_in, open(f_output, 'w') as f_out:
for line in f_in:
f_out.write(line)
with open(f_input, 'r') as f_in, open(f_output, 'a') as f_out:
for line in f_in:
f_out.write(line)
except:
print("Error: ", sys.exc_info())