[Tutor] Append mode dilemma
Luke Paireepinart
rabidpoobear at gmail.com
Wed Dec 9 20:12:29 CET 2009
On Wed, Dec 9, 2009 at 1:02 PM, biboy mendz <bibsmendez at gmail.com> wrote:
>
> Luke Paireepinart wrote:
>> That's because there is NOT a new line at the end of the file.
>> It's a file you're appending to, it's up to YOU to create that new line.
>> And all files should end with newlines anyway (on linux).
>> So modify your code so that you output a new line at the end of your
>> outputs.
>>
>> fobj = open(fname, 'a')
>> fobj.write('\n'.join(all))
>> fobj.close()
>>
>>
> Quickie hack just to achieve the newline. Doesn't python have a function
> that adds newline automatically in append mode?
Why would it need a function for this? Some people might not want a
newline, so it can't be automatically enabled.
>
>
>
> fobj = open(fname, 'a')
> fobj.write('\n'.join(all))
> print
> fobj.close()
This won't work unless you have STDOUT redirected to your file. It would be
better just to do
fobj.write('\n'.join(all) + '\n')
which is shorter than a function call that would enable this feature anyway.
This isn't a hack, this is how you're supposed to do this.
-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091209/84ea49f2/attachment.htm>
More information about the Tutor
mailing list