943,551 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 9860
  • Python RSS
Apr 5th, 2009
0

AttributeError: 'str' object has no attribute 'insert'

Expand Post »
I'm fairly new to python and object oriented programming in general, so I'm having a little trouble figuring out why this doesn't work

this is the error I'm getting for the code below:
Error: 'str' object has no attribute 'insert'

Python Syntax (Toggle Plain Text)
  1. filetolist = FileReadToList("test.txt")
  2. print filetolist[0]
  3.  
  4. def FileCountLines(s_filename):
  5. f = open(s_filename)
  6. filestring = f.read()
  7. count = operator.countOf(filestring, "\n")
  8. file.close(f)
  9. return count
  10.  
  11. def FileReadToList(s_filename):
  12. f = open(s_filename)
  13. count = FileCountLines(s_filename)
  14. filestring = f.read()
  15. filestring.insert(0,count)
  16. file.close(f)
  17. return filestring
Last edited by MaQleod; Apr 5th, 2009 at 11:33 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MaQleod is offline Offline
3 posts
since Apr 2009
Apr 6th, 2009
0

Re: AttributeError: 'str' object has no attribute 'insert'

Change
Python Syntax (Toggle Plain Text)
  1. filestring = f.read()
to
Python Syntax (Toggle Plain Text)
  1. filestring = f.readlines()

-BV
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Apr 6th, 2009
0

Re: AttributeError: 'str' object has no attribute 'insert'

thanks, that worked, why does that make a difference?
I'd like to understand exactly what is going on.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MaQleod is offline Offline
3 posts
since Apr 2009
Apr 6th, 2009
0

Re: AttributeError: 'str' object has no attribute 'insert'

The readlines() file method returns a list. The read() method returns a string. A str object has no insert() method but a list object does.
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Apr 6th, 2009
0

Re: AttributeError: 'str' object has no attribute 'insert'

makes sense, thanks again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MaQleod is offline Offline
3 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: A Beginner's Question
Next Thread in Python Forum Timeline: Taking a variable out of a Tkinter GUI





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC