# Ex4
# ---
# Viết function tạo ra 1 file chứa 30 triệu dòng, các dòng lẻ chứa 30 số 1,
# các dòng chẵn chứa giá trị 2 * số dòng hiện tại.
# Viết function in ra 10 dòng cuối cùng của file nói trên.
# (Nâng cao) Viết function in ra kích thước của file nói trên tính theo byte.
def create_file(filename):
with open(filename, 'w') as f:
for line in range(30000000):
if line % 2 != 0:
f.write(30 * '1' + '\n')
else:
f.write(str(line * 2) + '\n')
def last_ten_lines(filename):
lines = []
with open(filename) as f:
for line in f:
lines.append(line)
if len(lines) > 10:
lines.pop(0)
for row in lines:
print(row, end='')
def file_size(filename):
import os
print('File size: %s byte' % os.path.getsize(filename))
create_file(filename)
last_ten_lines(filename)
file_size(filename)