Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20473
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Subject | Re: writing to a file from within nested loops |
| Date | 2012-02-15 20:44 +0000 |
| References | (4 earlier) <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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5854.1329338641.27778.python-list@python.org> (permalink) |
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<m
> r=name[i]
> f=open('file'+'%s' %(r), "a")
> f.write("whatever"+r) #part outside innermost loop gets printed
> j=0
> while j<n
> 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.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: writing to a file from within nested loops Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-02-15 20:44 +0000
csiph-web