1

If there was a text file in which each line might have a different length, how would one format each line of the file to be a certain number of characters in length?

For example, turn

>Letters of the alphabet
ABCDEFGHIJKLMNOPQRSTUVWXYZ

to

>Letters of the alphabet
ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
Z

and so on as such.

Russ
  • 103
  • 9

1 Answers1

0

In python 3 :

# -*- coding: utf-8 -*-

max_length = int(input("Max length of your line = "))

file = open("file.txt","r") #open the file to edit and read the data
content = file.read()
file.close()

file_out = open("file_out.txt","w+") #create the output file

for i in range(len(content)):
    file_out.write(content[i])
    if (i + 1) % max_length == 0: #add line break each time pgcd equal 0
         file_out.write('\n')
file_out.close()
Taknok
  • 717
  • 7
  • 16