Text12. Дана строка S и текстовый файл. Заменить в файле все пустые строки на строку S.

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

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

def RndString():
ru_letters = u"абвгдеёзийклмнопрстуфхъыьэАБВГДЕЁЗИЙКЛМНОПРСТУФХЪЫЬЭ"
en_letters = u"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
tj_letters = u"ғӣқӯҳҷҒӢҚӮҲҶ"

N = random.randrange(1,10)
s_ru = random.sample(ru_letters, N)
s_en = random.sample(en_letters, N)
s_tj = random.sample(tj_letters, N)
s_digit = random.sample(string.digits, N)
s = s_ru + s_en + s_tj + s_digit + [' ',' ']
random.shuffle(s)
rnd_str = "".join(s)
return rnd_str


#file for reading and adding empty row
file1 = "text12_1.txt"

#file for temporary data
N = random.randrange(5,8)
S = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
temp_file = "temp_" + S + ".txt"
temp_file = "text12_2.txt"
print("Temp file:",temp_file)
print("Write to:",temp_file)

rnd_str = RndString()
print("String:",rnd_str)

with codecs.open(file1, 'r', 'utf-8') as infile:
with codecs.open(temp_file, 'w', 'utf-8') as outfile:
for line in infile:
if line.strip("\r\n") != "":
outfile.write(line)
else:
outfile.write(rnd_str+"\r\n")
sys.exit()

print("Rename {0} to {1}".format(temp_file,file1))
try:
os.rename(temp_file, file1)
except WindowsError:
os.remove(file1)
os.rename(temp_file, file1)
except OSError as e:
print("Error:", e)