Text5. Дана строка S и текстовый файл. Добавить строку S в конец файла.

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

import random
import string

def AppendLine(fname,line):
try:
f = open(fname, "a")
try:
f.write(line+"\n")
finally:
f.close()
except IOError:
print('Write error: ',fname)


N = random.randrange(1,20)
S = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
print("S:",S)

file_name = "text05.txt"
AppendLine(file_name,S)