Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Karim Newsgroups: comp.lang.python Subject: Re: Writing different sections into a file Date: Mon, 25 Apr 2016 12:04:27 +0200 Lines: 34 Message-ID: References: <571DEBAB.6010503@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de sYnLpSDuuv/ZK6NVIlYxLAaDk1AVacf+tpVq+YEa7YHQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:file': 0.07; 'stringio': 0.09; 'subject:Writing': 0.09; 'subject:into': 0.09; 'sections': 0.13; 'received:74.125.82.44': 0.15; 'section.': 0.15; "skip:' 30": 0.15; 'cstringio': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'stringio()': 0.16; 'wrote:': 0.16; 'file.': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header': 0.24; 'header:User-Agent:1': 0.26; 'point': 0.33; 'message-id:@gmail.com': 0.34; 'file': 0.34; 'this?': 0.34; 'received:google.com': 0.35; 'received:74.125.82': 0.35; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'hi,': 0.38; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'charset:windows-1252': 0.62; 'different': 0.63 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=3JticJmG6pxXLfuUwK9nFHatlHt07ey91HZe8zv70K4=; b=V0ysP59rqAsos45P3Fe20zlsmAzvgO5z+B+BlEOWTMr1vp24PYm6m+aEirj4SB9TZB ziLZiLtXY9AgWuYbLoMKCU6PS8TsMnbkTV/oSBdHFvWTOqSmXxDivy6TCE5n5ekZjoS3 9YKcKI3cE2N97LNCZfPv8vmlIjvzBCgMwSSzZHpKXfVmgsAB/G/2jsc7L3REwOLNN6u5 mApoYP/ZW7w9yl2KCK+BMHXccQ3Yv8L2NQnI+sm6NMMpv4hW9IcEaPraet4ZmGp6V/eo C8bSPm69+gaXI/nvFwYm/kPrchdzK3fJL6UpkCTQolwppNouJXT5pGWb+IHT6IKJE+ex wbEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=3JticJmG6pxXLfuUwK9nFHatlHt07ey91HZe8zv70K4=; b=kg+a1MTdbIYQiTPOKy9FFyNaX4d/lzXsC8+1Vborvmy8J6JOdzHE3kh6Z/ua/58qC8 hJp1TltKuXHNwfs6NcHcTUZgBfI5OLTObhxB3dqbq/e45SwXpx7HGJcdRa5Dc3XVyWt0 LYWcp+Gid/4lw8jJk00hZQvqtkzTe+KeqDEUMxNdj0+0wtXaxUQ9zask6UHTHG0vpT5D GlaKHvwDW6EoaP0r6hTaSeeCA5IfNB3GYbEM7raK62Cwz7plcpoT4+PF1UPDqjY+B0LI W5yefXDGPmLHtG3HiJwHn0rLx9BYyJ1QyNH4Ut5IbcwFSvhO+E941ymZ6tKSou4SBk/y 6V0A== X-Gm-Message-State: AOPr4FURE2sz3LlXrlo9/SDemeGfJjeXiMScUVf1rdWNIMXrj0d4D01JNMinFm0iIEIPiw== X-Received: by 10.28.18.81 with SMTP id 78mr10432749wms.59.1461578669597; Mon, 25 Apr 2016 03:04:29 -0700 (PDT) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <571DEBAB.6010503@gmail.com> X-Mailman-Original-References: Xref: csiph.com comp.lang.python:107592 On 25/04/2016 09:30, Palpandi wrote: > Hi, > > I need to write different sections into a file. > At any point of time, content can be added to any section. > > I don't want keep each section into a temporary file. > What is the better way to store the contents of each section and write them into a file at the end? > What is the better datatype to achieve this? > > > Thanks and Regards, > Palpandi Use Stringio: - from cStringIO import StringIO content = StringIO() # Header content.write('; Header\n') content.write('; Body'\n) content.write('; Footer\n') open('my_file', 'wb').write(content.getvalue()) - Karim