Text14. Дан непустой текстовый файл. Удалить из него последнюю строку.

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

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

file1 = "text14.txt"
temp_file = "text14_2.txt"
print("Read from:",file1)
print("Temp file:",temp_file)

try:
with open(file1, 'r') as infile:
lines = infile.readlines()
with open(temp_file, 'w') as outfile:
for line in lines[:-1:]:
outfile.write(line)
except IOError:
print('Open error: ',file1)

#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)