Text8. Даны два текстовых файла. Добавить в начало первого файла содержимое второго файла.
Решение на Python 3:
file1 = "text08_1.txt"
file2 = "text08_2.txt"
content1 = ""
try:
with open(file1,'r') as f:
content1 = f.read()
except IOError:
print('Open error: ',file1)
content2 = ""
try:
with open(file2,'r') as f:
content2 = f.read()
except IOError:
print('Open error: ',file2)
print("Write content #2 to ",file1)
try:
f = open(file1, "w")
try:
f.write(content2)
finally:
f.close()
except IOError:
print('Write error: ',file1)
print("Append content #1 to ",file1)
try:
f = open(file1, "a")
try:
f.write(content1)
finally:
f.close()
except IOError:
print('Append error: ',file1)