Change a file type in Python?

rusi rustompmody at
Sun Dec 1 04:02:09 CET 2013

On Sunday, December 1, 2013 5:34:11 AM UTC+5:30, Eamonn Rea wrote:
> Thanks for the help!
> Ok, I'll look into the mailing list.

[Assuming you are using GG with firefox on linux]

All you need to do is
1. Install 'Its all text' FF addon
2. Point the 'editor' of 'Its all text' to the below python script

After that, a small new edit button will appear outside the text-box and its one-click solution

-------------Python Script--------------
#!/usr/bin/env python3

# As far as I know both python2 and 3 work
# Windows/Mac no idea :-)

# A script to drop-in as an editor for firefox addon "Its all text"
# It cleans up two google-group nuisances:
# 1. Useless blank lines
# 2. Excessively long lines
# No efforts at error reporting as stderr is not available in any
# easy way (I know) to firefox (other browsers?)
# To test separately:
# Compose a mail (preferably reply) in GG
# Copy-paste the stuff (maybe with some long lines added without the >)
# Run this script with that filename as argv[1]

from sys import argv
from re import sub
import re

# Clean double spacing
def cleands(s):
    # Assumption: ASCII 025 (NAK) never occurs in input
    s1 = sub("^> *\n> *$", "\025"  , s , flags=re.M)
    s2 = sub("^> *\n"    , ""      , s1, flags=re.M)
    s3 = sub("\025\n"    , ">\n"   , s2, flags=re.M)
    return s3

# Maximum length that (new) lines should attain
Maxlen = 75

# clean all long lines, s is the whole file/text
def cleanall_ll(s):
    lines = (cleanll(l) for l in s.split("\n"))
    return "\n".join(lines)

# clean one long line
def cleanll(line):
    return ( line if line.startswith(">") else cleanll_rec(line) )

def cleanll_rec(line):
    if len(line) <= Maxlen : return line
    pos = line.rfind(" ", 0, Maxlen)
    if pos == -1 : #Failed due to no spaces
        return line
    return line[0:pos] + "\n" + cleanll_rec(line[pos+1: ])

def clean(s):
    return cleanall_ll(cleands(s))
def main():
    with open(argv[1])      as f: s =
    with open(argv[1], "w") as f: f.write(clean(s))

if __name__ == '__main__' :

More information about the Python-list mailing list