Skip to content

Instantly share code, notes, and snippets.

@wincentbalin
Created August 1, 2018 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wincentbalin/85707ce703b1e4bc0737ed569fb16bea to your computer and use it in GitHub Desktop.
Save wincentbalin/85707ce703b1e4bc0737ed569fb16bea to your computer and use it in GitHub Desktop.
Text file rewrapping script
#!/usr/bin/env python3
"""Rewrap lines without breaking words"""
import sys
import argparse
import textwrap
argparser = argparse.ArgumentParser(description=sys.modules[__name__].__doc__)
argparser.add_argument('infile', type=argparse.FileType('r', encoding='UTF-8'))
argparser.add_argument('outfile', type=argparse.FileType('w', encoding='UTF-8'))
argparser.add_argument('width', type=int)
args = argparser.parse_args()
for line in args.infile:
for wrapped in textwrap.wrap(line, args.width):
args.outfile.write('{}\n'.format(wrapped))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment