Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.07; 'whatever.': 0.07; 'python': 0.08; 'advance.': 0.09; 'confuse': 0.09; 'exception.': 0.09; 'loop.': 0.09; 'negative,': 0.09; 'error:': 0.10; 'output': 0.10; 'subject:file': 0.13; '"a")': 0.16; 'closes': 0.16; 'f.close()': 0.16; 'flushed': 0.16; 'innermost': 0.16; 'received:192.168.1.104': 0.16; 'subject:loops': 0.16; 'subject:writing': 0.16; 'traceback.': 0.16; 'cc:addr:python-list': 0.16; 'syntax': 0.16; 'wrote:': 0.18; 'errors,': 0.18; 'of.': 0.18; 'once,': 0.18; 'help.': 0.19; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'btw,': 0.23; 'fine,': 0.23; 'happen.': 0.23; 'loop,': 0.23; 'string': 0.24; 'object,': 0.24; 'guess': 0.26; 'somewhere': 0.26; 'cc:2**0': 0.26; 'code': 0.26; 'code,': 0.28; 'column': 0.28; "i'm": 0.28; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'correct': 0.29; 'pm,': 0.29; 'object.': 0.30; "didn't": 0.30; 'file.': 0.31; 'version': 0.32; 'minor': 0.32; 'thanks': 0.32; 'actual': 0.32; 'error.': 0.32; "isn't": 0.33; 'sort': 0.33; 'there': 0.33; 'header:User-Agent:1': 0.33; 'it.': 0.33; 'file': 0.34; 'loop': 0.34; 'wondering': 0.34; 'hi,': 0.34; 'someone': 0.34; 'be,': 0.34; 'list.': 0.35; "we're": 0.36; 'run': 0.37; 'but': 0.37; 'received:192': 0.38; 'could': 0.38; 'some': 0.38; 'think': 0.38; 'getting': 0.38; 'received:192.168.1': 0.39; 'subject:from': 0.39; 'mailing': 0.39; 'recommended': 0.39; 'might': 0.40; "you've": 0.61; 'your': 0.61; 'here': 0.64; 'show': 0.66; 'cause': 0.67; 'header:Reply-To:1': 0.70; 'reply-to:no real name:2**0': 0.72; 'spaces': 0.73; 'calls,': 0.84; 'quote,': 0.84; 'show,': 0.84; 'redefining': 0.91; 'retype': 0.91 Date: Wed, 15 Feb 2012 15:36:31 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 Thunderbird/3.1.16 MIME-Version: 1.0 To: Rituparna Sengupta Subject: Re: writing to a file from within nested loops 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> In-Reply-To: <77a0e187170dca.4f3bbd59@wiscmail.wisc.edu> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:lkmxzCnxj3hhXG53hPkkjpMyEnAljCnBxKDFfXU7bHd 54Ufr5R55EicYDNcuIA98tnG02uvt0BWgBdOEsEozM0lh089+p SXJajAh/qJE2Q+iL/rKw1zog3yn/Y4K6O+DAbUAvbsymY02TXy G2u0EDZNnt8HBxr43dGh5KRCoyO7bWpv9TDMZzCdtcinZWn1qG aArpI5X/Mv2J8ney+bTIm/Yt6CzGI1zyG1IC4HGJfUCpOukme0 eyBlHzwWAa1mUikw5PED7PYtOX99dyB7aQSODd4WDtmo3RE47/ CzooFe+lhQkMiJa3TAViEunlWY7rJQXchAdImYsFlE8z1ZDHw= = Cc: python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name 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: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1329338227 news.xs4all.nl 6953 [2001:888:2000:d::a6]:58487 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20472 On 02/15/2012 03:12 PM, 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 > > Welcome to the mailing list. Some fundamentals, please: 1) give python version & os 2) copy/paste the code, don't retype it. You have lots of typos which would cause syntax errors, not "xxx" has no attribute "yyy". And please don't use 1 column indents; they're illegible. 4 spaces is a good number, recommended in a large number of places, including pep8. 3) copy/paste the whole error, including traceback. Without it in this case, we're forced to guess where the problem might be, and that must be somewhere else in the program, since the only write() attribute you've quoted here are on f.write() calls, and f is only set to a file object, not a string object. There are lots of ways in the code you don't show, where you might confuse the system, such as redefining open. Now to your questions: A) Since you don't show n, it could very well be 0 or negative, in which case the inner loop would never execute. B) Since you don't have correct indentation, it's possible the actual program closes the file inside the inner loop, in which case it might execute just once, then get an exception. not the one you quote, but whatever. C) Depending on how you run this program, perhaps the file isn't getting flushed when you get your error, so you just think the output didn't happen. D) BTW, i don't see any prints, but perhaps that's a minor point. You might think of file.write() as printing to a file. Sort of. -- DaveA