[Pythonmac-SIG] os.linesep?

Joseph J. Strout joe@strout.net
Tue, 2 Nov 1999 11:50:09 -0800


At 2:04 PM -0500 11/2/99, Chris,Mann wrote:

>No, here's the bit of code that's writing out to a file:
>         file = file + '.all'
>         fileID = open(file, 'w')
>         for bug in self.bugs:
>             if not bug.err:
>                 fileID.write(bug.summary(delimiter))
>                 fileID.write(linesep)
>                 iBugCount = iBugCount + 1
>         fileID.close()

This is fine as long as linesep is '\n', not os.linesep.  As Just 
explained, files opened in text mode (like the above) swap '\n' and 
'\r' when reading and writing.  So always use '\n'.

Note that this could be written more concisely as:

   fileID.writelines(map(lambda x,d=delimiter:x.summary(d), self.bugs))

in place of the "for" loop.  Oh, and then do iBugCount = iBugCount + 
len(self.bugs).

Cheers,
-- Joe

,------------------------------------------------------------------.
|    Joseph J. Strout           Biocomputing -- The Salk Institute |
|    joe@strout.net             http://www.strout.net              |
`------------------------------------------------------------------'