Попробуйте, с test.py
в качестве входного скрипта:
x = [e.strip() for e in open("test.py", "r").readlines()] # get lines, strip them of the '\n'
r = [(x[i]+'\n') * 3 for i in range(len(x))] # multiply each line by three
with open("test_out.py", "w") as out: # open an output, writing mode
out.write(''.join(r)) # join the lines, write to output
out.close() # close the file (keep things clean)
Обрезка каждой строки '\ n' (которая может быть там или нет в самой последней строке), чтобы обеспечить последнюю строку всегда пишется правильно, то есть не как
print("Stack Overflow is amazing")print("Stack Overflow is amazing")print("Stack Overflow is amazing")
, а как
print("Stack Overflow is amazing")
print("Stack Overflow is amazing")
print("Stack Overflow is amazing")
, если возврат каретки отсутствует.
Не забудьте изменить имя выходного файла на фактический путь, если вы хотите, чтобы ваш выходной файл был где-то еще!