Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Grant Edwards Newsgroups: comp.lang.python Subject: Re: Writing different sections into a file Date: Mon, 25 Apr 2016 14:44:37 +0000 (UTC) Lines: 29 Message-ID: References: X-Trace: news.uni-berlin.de IyBSw9V9DVrTgSQ1JaDd/QtnSgQKO8Y/q/8Rd1tQQpJw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'append': 0.07; 'subject:file': 0.07; 'footer.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:qwest.net': 0.09; 'section,': 0.09; 'stringio': 0.09; 'subject:Writing': 0.09; 'subject:into': 0.09; 'sections': 0.13; 'section.': 0.15; 'files).': 0.16; 'first:': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'why,': 0.16; 'wrote:': 0.16; 'string': 0.17; 'body,': 0.22; 'file.': 0.22; '(or': 0.23; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'header,': 0.29; 'point': 0.33; 'file': 0.34; 'this?': 0.34; 'list': 0.34; 'next': 0.35; "isn't": 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'desired': 0.37; 'done.': 0.37; 'hi,': 0.38; 'end': 0.39; 'data': 0.39; 'to:addr:python.org': 0.40; 'different': 0.63; 'life': 0.67; 'best?': 0.84; 'edwards': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 67-130-15-94.dia.static.qwest.net User-Agent: slrn/1.0.2 (Linux) 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: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:107608 On 2016-04-25, 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 makes you think a temporary file isn't best? > What is the better datatype to achieve this? When in doubt, try the simplest approach first: Use a string (or byte-string if it's not text) for each of the header, body, and footer. Append data to each as desired and then write them out to a file when you're done. If that's not workable, explain why, and we can tell you what to try next (probably a stringio for each section, or a list of strings or byte-strings for each section, or temporary files). -- Grant Edwards grant.b.edwards Yow! Wait ... is this a FUN at THING or the END of LIFE in gmail.com Petticoat Junction??