remove text wrapping in a text file in python

You can remove text wrapping in a text file using Python by reading the file line by line and concatenating lines that end with a newline character with the next line. Here's an example:

main.py
with open('filename.txt', 'r') as f:
    lines = f.readlines()

new_lines = []
current_line = ''
for line in lines:
    if line.endswith('\n'):
        current_line += line.strip()
        new_lines.append(current_line)
        current_line = ''
    else:
        current_line += line.strip()

# Write the new lines to the file
with open('filename.txt', 'w') as f:
    f.write('\n'.join(new_lines))
399 chars
17 lines

This code reads the file line by line and concatenate the current line with the next line if the current line ends with a newline character. The new lines without text wrapping are then written back to the same file.

Note that this will modify the original file. If you want to preserve the original file, you can write the new lines to a new file instead.

gistlibby LogSnag