[Tutor] Append mode dilemma

biboy mendz bibsmendez at gmail.com
Wed Dec 9 20:02:22 CET 2009

> 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?

fobj = open(fname, 'a')

bibs M.

Host/Kernel/OS  "cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 
[sidux 2009-02 Αιθήρ - kde-full - (200907141427) ]

>> Are you aware of how 'join' works?
>> try
>> print "#".join(['a','b','c'])
>> at the interactive prompt and see if you can't figure out why you're 
>> missing a newline at the end of your output.
>> HTH,
>> -Luke
> Hi Luke,
> Thank you. To be honest I'm confused of the different string methods 
> like join(), split(), etc. Anyway I will practice them to see how they 
> work.

More information about the Tutor mailing list