help me to find the error
davea at ieee.org
Fri Jul 10 11:57:21 EDT 2009
jhinak sen wrote:
> thanx a lot :)
> i got ur points .. and it really helps..
> and please also tell me ...
> where i can get more basic and detail knowledge of python.. as i am
> beginners in this , i need more examples of python programmes so that i can
> understand better.
> also if you know of any gud pdf file or book please let me know
> thnx a lot
> On Fri, Jul 10, 2009 at 7:25 PM, Dave Angel <davea at ieee.org> wrote:
>> jhinak sen wrote:
>>> i am a beginner in python language,
>>> i am trying with this programme :
>>> to find the addition and mean from a data set in a file and writing the
>>> and sum in some other file :
>>> *#! /usr/bin/env python
>>> import re
>>> import cPickle as p
>>> import math
>>> from numpy import *
>>> f0= open("temp9","r+").readlines()
>>> f2= open("out1","r+")
>>> add_1=[ ];
>>> for i in range(0, len(f0)):
>>> add= float(f1)+float(f1)
>>> mean= float(add)/2
>>> print (f1).ljust(6) ,(f1).ljust(6),repr(add).ljust(7),
>>> f2.write("%s" % repr(add).ljust(7)),f2.write("%s" %
>>> print "printing from file"
>>> for i in range(0, len(add_1),2):
>>> print add_1[i]," ", add_1[i+1]
>>> and this programme is givving me this error :
>>> "" *Traceback (most recent call last):
>>> File "./temporary1.py", line 24, in <module>
>>> AttributeError: 'list' object has no attribute 'close'*
>>> please help to to find the error.
>>> or suggest some simpler or better way
>>> 1)file "temp9" is already exist
>>> 2)this programme is giving me all my outputs, but at the end of the out
>>> ..its giving me that error.
>> Others have pointed out the specific problem that gives you this error.
>> But I'd like to point out a few other things to consider:
>> 1) Don't mix tabs and spaces. Best practice is to bind tab to (4) spaces
>> in your editor, and never have a tab in a Python source file.
>> 2) Think about your variable names. As it stands, f0 is a list of lines,
>> f1 is a list of "word" within a line, and f2 is a file. No wonder you
>> accidentally tried to close the list. I'd suggest things like:
>> infile = open(....)
>> lines = infile.readlines()
>> outfile = open(....)
>> for line in lines:
>> words = line.split(" ") or even val1, val2 =
>> lines.split(" ")
>> Then of course the last two lines become
>> 3) Learn to use the for statement directly on a list, rather than using
>> len() on the list to make an index, then using the index to find the value
>> 4) On the open() calls, get your modes right. Looks like you really want
>> infile = open(infilename, "r")
>> outfile = open(outfilename, "w")
>> 5) Consider using tuples in your add_1 list, rather than separate elements.
>> That way, each element of the list would contain both sum and mean.
>> add_1.append((add, mean))
>> and the final print would become
>> for item in add_1:
>> print item," ", item
>> 6) Put anything over three lines into a function, instead of doing it at
>> module scope. That way, you'll be clearer about what things are local to
>> this code, and what might be useful to other code in the same module.
>> In this case, infilename, and outfilename might be arguments to that
>> There are lots of other refinements, but these are all within your reach,
>> and would make the program much clearer.
Please don' t top-post. Putting your reply out of order makes it harder
for others to see the sequences of things. Some people top-post
everything, but on this mailing list (and maybe most), the standard is
to add to bottom, or inline where appropriate.
are all good, depending on your experience with other languages, and
with your computer's OS.
You could also check out
http://code.activestate.com/recipes/langs/python/ for Python specifically
which has a large set of relatively small modules of code.
For a examples that might stretch your thought process:
More information about the Python-list