Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed10.multikabel.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'string.': 0.04; 'python': 0.08; '*you*': 0.09; 'alot': 0.09; 'newline': 0.09; 'none.': 0.09; 'skip:f 30': 0.13; 'cc:addr:python-list': 0.15; '"\\n")': 0.16; 'newlines': 0.16; 'nick': 0.16; 'works...': 0.16; 'wrote:': 0.16; 'subject:Help': 0.17; 'trying': 0.20; '(but': 0.21; 'header:In- Reply-To:1': 0.22; 'figure': 0.23; 'skip:[ 10': 0.27; 'tried': 0.27; 'producing': 0.28; 'elements': 0.28; 'cc:addr:python.org': 0.29; 'object.': 0.30; 'skip:\xc2 20': 0.30; 'strings.': 0.30; 'equivalent': 0.30; 'everyone,': 0.32; 'list': 0.32; 'object': 0.33; 'file.': 0.34; 'calling': 0.34; '...': 0.35; 'file': 0.35; 'cc:2**1': 0.36; 'sequence': 0.36; 'but': 0.37; 'could': 0.37; 'skip:o 20': 0.38; 'e.g.': 0.38; 'help': 0.39; 'subject:: ': 0.39; '8bit%:8': 0.40; 'join': 0.61; 'header:Received:6': 0.61; 'skip:w 30': 0.63; 'reply-to:no real name:2**0': 0.72; 'header:Reply- to:1': 0.84; '8bit%:16': 0.84 To: Anatoli Hristov From: Nick Dokos Subject: Re: Help writelines In-Reply-To: Message from Anatoli Hristov of "Fri\, 03 Feb 2012 21\:27\:48 +0100." References: Organization: HPCS X-Mailer: MH-E 8.3.1; nmh 1.3; GNU Emacs 24.0.92 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 03 Feb 2012 15:47:37 -0500 Sender: nicholas.dokos@hp.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: nicholas.dokos@hp.com List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328302606 news.xs4all.nl 6947 [2001:888:2000:d::a6]:40311 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19843 Anatoli Hristov wrote: > Hi everyone, >=20 > I`m totaly new in python and trying to figure out - how to write a list t= o a file with a newline at the end of each object. > I tried alot of combinations :) like: > users =3D ['toli','didi'] > fob=3Dopen('c:/Python27/Toli/username','w') > fob.writelines(users) + '%s\N' > fob.close() > =C2=A0or=C2=A0fob.writelines('\N' % users)=C2=A0 > or=C2=A0fob.writelines('%s\N' % users) > but nothing of dose works... >=20 > Could you help me find out the right syntaxes? >=20 >From the docs: | writelines(...) | writelines(sequence_of_strings) -> None. Write the strings to the = file. |=20=20=20=20=20=20 | Note that newlines are not added. The sequence can be any iterable= object | producing strings. This is equivalent to calling write() for each s= tring. So *you* need to add the newlines, e.g. you can use a list comprehension: fob.writelines(["%s\n" % (x) for x in users]) or write in a loop: for u in users: fob.write("%s\n" % (u)) or join the list elements together with a newline separator (but you'll need to add a final newline by hand): fob.writelines("\n".join(users) + "\n") or ... Nick