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'

filetolist = FileReadToList("test.txt")
print filetolist[0]

def FileCountLines(s_filename):
	f = open(s_filename)
	filestring = f.read()
	count = operator.countOf(filestring, "\n")
	file.close(f)
	return count

def FileReadToList(s_filename):
	f = open(s_filename)
	count = FileCountLines(s_filename)
	filestring = f.read()
	filestring.insert(0,count)
	file.close(f)
	return filestring

Recommended Answers

All 4 Replies

Change

filestring = f.read()

to

filestring = f.readlines()

-BV

thanks, that worked, why does that make a difference?
I'd like to understand exactly what is going on.

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.

makes sense, thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.