Re: [Tutor] re Format a file
prasad rao prasadarao...@gmail.com wrote for line in so: if len(line)70:de.write(line+'\n') if len(line)70: da=textwrap.fill(line,width=60) de.write(da+'\n') What happens if the line is exactly 70 characters long? I think you want an else instead of the second if -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re Format a file
On Fri, Feb 27, 2009 at 5:09 AM, prasad rao prasadarao...@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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re Format a file
2009/2/27 prasad rao prasadarao...@gmail.com: Hello I don't know why, but this I think going into infinite loop. I cant see anything wrong in it. Please show me where the problem is. [...] while len(line)60: tem=line[60:] try: ??? a,b=tem.split(' ',1) ??? de.write(line[:60]+a+'\n') ??? line=b except ValueError:pass So you have a while loop, whose condition depends on line. This will loop infinitely if the body of the loop does not change the value of line. Can you see how that could happen? -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re Format a file
On Fri, 27 Feb 2009 09:59:40 +0530, prasad rao wrote: def myform(s): import os so=open(s) d=os.path.dirname(s)+os.sep+'temp.txt' de=open(d,'w') for line in so: while len(line)60: item=line[60:] try: a,b=tem.split(' ',1) what is tem here? It must be a global, since I can't see any other reference to tem in the function definition. If tem is global, then the value of b (a.k.a. line) will never change (except for the first iteration) de.write(line[:60]+a+'\n') line=b except ValueError: pass de.write(line+'\n') so.close() de.close() os.remove(s) os.rename(d,s) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor