Skip to content

Instantly share code, notes, and snippets.

@robbyt
Created February 18, 2015 03:53
Show Gist options
  • Save robbyt/ac027a33d19877e2f676 to your computer and use it in GitHub Desktop.
Save robbyt/ac027a33d19877e2f676 to your computer and use it in GitHub Desktop.
Simple script to rewrite and wrap text files at 80 chars. Great for `.txt` files and `.md` files.
#!/usr/bin/env python
import sys
import textwrap
WIDTH = 79
def main(argv):
if len(argv) != 2:
sys.exit("Error, please pass a single file name you wish to wrap")
new_name = "80_" + argv[1]
with open(argv[1]) as orig:
with open(new_name, "w") as new:
for line in orig:
if line == "\n":
new.write("\n")
else:
new_text = textwrap.fill(line, width=WIDTH)
if line.endswith("\n") and not new_text.endswith("\n"):
new_text += "\n"
new.write(new_text)
if __name__ == '__main__':
main(sys.argv)
@mache
Copy link

mache commented Feb 23, 2015

Nice! 😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment