Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.07; 'snippet': 0.07; 'python': 0.08; 'advance.': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'error:': 0.10; 'subject:file': 0.13; '"a")': 0.16; 'f.close()': 0.16; 'guessing': 0.16; 'innermost': 0.16; 'subject:loops': 0.16; 'subject:writing': 0.16; 'wrote:': 0.18; 'string,': 0.18; 'help.': 0.19; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'fine,': 0.23; 'traceback': 0.24; 'somewhere': 0.26; 'code': 0.26; "i'm": 0.28; 'thanks': 0.32; 'error.': 0.32; "isn't": 0.33; 'header:User-Agent:1': 0.33; 'header:X-Complaints-To:1': 0.34; 'from:addr:yahoo.co.uk': 0.34; 'loop': 0.34; 'wondering': 0.34; 'hi,': 0.34; 'someone': 0.34; 'to:addr:python-list': 0.35; 'post': 0.36; 'received:org': 0.36; 'but': 0.37; 'could': 0.38; 'some': 0.38; 'getting': 0.38; 'subject:from': 0.39; 'received:78': 0.40; 'might': 0.40; 'to:addr:python.org': 0.40; 'your': 0.61; 'show': 0.66; 'received:as13285.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: writing to a file from within nested loops Date: Wed, 15 Feb 2012 20:44:02 +0000 References: <75208cce174437.4f3bf1a2@wiscmail.wisc.edu> <77a0ddfa17203f.4f3bf1de@wiscmail.wisc.edu> <775093eb1740b6.4f3bf21b@wiscmail.wisc.edu> <7760fe7a17153d.4f3bf258@wiscmail.wisc.edu> <75909037172f2d.4f3bf294@wiscmail.wisc.edu> <76f0965b175c1f.4f3bf2d0@wiscmail.wisc.edu> <77c0ac1d173db4.4f3bf403@wiscmail.wisc.edu> <7750b096170a1e.4f3c11b5@wiscmail.wisc.edu> <77a0e187170dca.4f3bbd59@wiscmail.wisc.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-146-8-223.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:10.0.1) Gecko/20120208 Thunderbird/10.0.1 In-Reply-To: <77a0e187170dca.4f3bbd59@wiscmail.wisc.edu> X-Antivirus: avast! (VPS 120215-0, 15/02/2012), Outbound message X-Antivirus-Status: Clean X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1329338641 news.xs4all.nl 6847 [2001:888:2000:d::a6]:34072 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20473 On 15/02/2012 20:12, Rituparna Sengupta wrote: > Hi, > > I'm working on this code and I keep getting an error. It might be some very basic thing but I was wondering if someone could help. Its a loop within a loop. The part outside the innermost loop gets printed fine, but the part within the innermost loop doesn't get printed. I get an error: 'str' has no attribute 'write'. Thanks in advance. > > Ritu > > . > i=0 > while i r=name[i] > f=open('file'+'%s' %(r), "a") > f.write("whatever"+r) #part outside innermost loop gets printed > j=0 > while j f.write("output of loop") #part within innermost loop doesn't get printed > j=j+1 > f.close() > i=i+1 > The above isn't Python so please post all of your code or a representative snippet that can show the problem, with the complete traceback and not simply the last line. Having said that I'm guessing that you're reassigning f somewhere to be a string, hence the error. Also why not write Python loops like:- for r in name: etc. -- Cheers. Mark Lawrence.