The fundamentals...

Nils Hensel nils.hensel at online.de
Wed Jan 24 18:42:50 EST 2001


Hi Mike,

import string
boo = string.join (open('\\Windows\\Desktop\\test.txt').read().split('.'),
'\n')
open('\\Windows\\Desktop\\out.txt', 'w').write(boo)

is definitely shorter. I don't know if I consider it to be more elegant.
Anayway, you should note that the string object returned by the read
function now has a builtin split function, refering to itself, and that you
need not close the file if it's not assigned to a variable. The file closes
itself when there are no more references to it. And if you prefer to close
your files explicitely don't forget to close the "test" file too :)
Enjoy Python,

Nils


"Mike Read" <mread at cableinet.co.uk> schrieb im Newsbeitrag
news:F8Jb6.30683$pp2.2619941 at news3.cableinet.net...
> Newbie first script alert!  No problem - it does what I intended (which
> probably says more about Python than me...), but I just wanted to start
> off on the right foot - is it as elegant as it could be?  If it's not
blindingly
> obvious, I was separating fields around a period delimiter.
>
> Cheers,
>
> Mike
>
>
> import string
> test = open('\\Windows\\Desktop\\test.txt', 'r')
> boo = test.read()
> boo = string.split(boo , ". ")
> boo = string.join(boo , "\n")
>
> outfile = open('\\Windows\\Desktop\\out.txt', 'w')
> outfile.write(boo)
> outfile.close()
>
>
>





More information about the Python-list mailing list