wrap all lines that are longer than line length

Posted by user1919840 on Stack Overflow See other posts from Stack Overflow or by user1919840
Published on 2013-11-12T03:50:58Z Indexed on 2013/11/12 3:52 UTC
Read the original article Hit count: 100

I am writing a program that limits each line to a certain length.

this is what i got so far, i am almost done but i still need to cut each line, but i cant figure it out.

def main():
    filename = input("Please enter the name of the file to be used: ")
    openFile = open(filename, 'r+')
    file = openFile.read()
    lLength = int(input("enter a number between 10 & 20: "))

    while (lLength < 10) or (lLength > 20) :
        print("Invalid input, please try again...")
        lLength = int(input("enter a number between 10 & 20: "))

    wr = textwrap.TextWrapper()
    wraped = wr.wrap(file)

    print("Here is your output formated to a max of", lLength, "characters per line: ")

    wr.width = lLength
    wr.expand_tabs = True
    for lines in wraped:
        print(lines)

an example of what the output SHOULD be is this. If the file specified contains this text:

hgytuinghdt #here the length is 11 ughtnjuiknshfyth #here the length is 16 nmjhkaiolgytuhngjuin #here the length is 20

and the lLength is specified to 15 then this should print out:

hgytuinghdt ughtnjuiknshfyt h nmjhkaiolgytuhng juin

Thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about python-3.x