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

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2009
Posts: 3
Reputation: MaQleod is an unknown quantity at this point 
Solved Threads: 0
MaQleod MaQleod is offline Offline
Newbie Poster

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

 
0
  #1
Apr 5th, 2009
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'

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

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

 
0
  #2
Apr 6th, 2009
Change
  1. filestring = f.read()
to
  1. filestring = f.readlines()

-BV
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: MaQleod is an unknown quantity at this point 
Solved Threads: 0
MaQleod MaQleod is offline Offline
Newbie Poster

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

 
0
  #3
Apr 6th, 2009
thanks, that worked, why does that make a difference?
I'd like to understand exactly what is going on.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

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

 
0
  #4
Apr 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: MaQleod is an unknown quantity at this point 
Solved Threads: 0
MaQleod MaQleod is offline Offline
Newbie Poster

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

 
0
  #5
Apr 6th, 2009
makes sense, thanks again
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC