[Tutor] re Format a file
Kent Johnson
kent37 at tds.net
Fri Feb 27 23:01:12 CET 2009
On Fri, Feb 27, 2009 at 5:09 AM, prasad rao <prasadaraon50 at gmail.com> wrote:
> Hello
> Finally I managed to writ a function to format a file.
> Thank to everybody for their tips.
>
> def mmm(a):
> ???? import os,textwrap
> ???? so=open(a)
> ???? d=os.path.dirname(a)+os.sep+'temp.txt'
> ???? de=open(d,'w')
> ???? import textwrap
> ???? for line in so:
> ???????? if len(line)<70:de.write(line+'\n')
You are introducing an extra newline here, line already ends in a newline.
> ???????? if len(line)>70:
> ???????????? da=textwrap.fill(line,width=60)
> ???????????? de.write(da+'\n')
Not sure if textwrap strips the trailing newline, if not, this is also
double-spacing.
Kent
More information about the Tutor
mailing list