8/9. Try it yourself, (add up)



Oops, try again. It looks like you didn't write any data to text.txt.

Traceback (most recent call last):
  File "python", line 5, in <module>
TypeError: expected a character buffer object

the exercise is too simple and as i read somewhere, to learn we should try doing additional things, so that what i tried,
i tried to write input using list, i am newbie so dont know it is actually going to work or not, so i thought asking here is better option. help me out :slight_smile:

Replace this line with your code.


What do you think the file object's write method expects you to give it as an argument? What are you giving to it?

Also, you could try running the same code in python3 where you would get a different error message that is easier to understand/more to the point.

Think about when you open the file, when does that happen in your program? What effect does that have?

You can also get a very brief description of that method by passing it as an argument to the help function.

Another source of documentation is using a search engine to search for python and the name of the method, perhaps the data type it's used on as well, and then look for hits on python.org. Things very central to the language can sometimes be difficult to find the documentation for though. This one can be found at https://docs.python.org/2/library/stdtypes.html#file-objects (use your browser's search function)


i am doing the same as what we did in "See it to Belive it" ( trying to enter square of every number from 1 to 10 in text.txt file)

my_list = [i**2 for i in range(1,11)]
# Generates a list of squares of the numbers 1 - 10

f = open("output.txt", "w")

for item in my_list:
    f.write(str(item) + "\n")


with help of using "with" "as"


You're not and you mustn't assume that you do because that hurts your ability to figure out what's wrong.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.