[Tutor] format a file

Kent Johnson kent37 at tds.net
Thu Feb 26 12:28:20 CET 2009


On Thu, Feb 26, 2009 at 4:09 AM, prasad rao <prasadaraon50 at gmail.com> wrote:
> hello
> I find it difficult to use horizontal scroll bar to read text documents.
> So I want to split lines in the text file in to two lines.
> <code>
> def  myforrmat(source,desty):
> ???? so=open(source)
> ???? de=open(desty,'w')
> ???? for line in so:
> ????????? if len(line)<60:de.write(line)
> ????????? if len(line)>60:
> ????????????de.write(''.join(list(line)[:60]))
> ????????????de.write(''.join(list(line)[60:]))

You don't have to convert to list, strings are sequences and support
slicing directly. As Lie notes, you have to write a newline between
the lines!
  de.write(line[:60])
  de.write('\n')
  de.write(line[60:])

Kent


More information about the Tutor mailing list