954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

character buffer object error

print "creating a text file"
text_file = open("write_it.txt", "a")
score = 5
name = raw_input("What's your name?: ")
score = (score, name)
text_file.write(score)
text_file.close()


getting TypeError: expected a character buffer object

Please help.
Thanks

abders
Newbie Poster
17 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

You must give write string argument.

print "creating a text file"
# 'w' mode creates new file
with open("write_it.txt", "w") as text_file:
  score = 5
  name = raw_input("What's your name?: ")
  text_file.write("%s, %s" % (score, name))
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Thanks alot pyTony.

You really helped me out.

By the way, I set the mode for "a" because I wanted to append the score, and as you know "a" also creates a new file.

But thanks alot for the help, now I can finish my whole program.

abders
Newbie Poster
17 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

Just so you know it's considered polite to mark the thread as solved so people can use it for future reference, and I'm sure pyTony wouldn't mind an upvote for his help if you feel it's in order.

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 

Thanks alot pyTony.

You really helped me out.

By the way, I set the mode for "a" because I wanted to append the score, and as you know "a" also creates a new file.

But thanks alot for the help, now I can finish my whole program.


Just consider that you will be allways addding to file in append mode, so you get 'log file' effect. You never clear it if it exists. Therefore it is more common to update file by reading it fully in, changing information in memory and again writing in 'w' mode to file.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Alright, thanks again Pytony for the tip.

Pyguy62, I am new here, and didn't really take time to mark it as 'solved', because I was so amazed of how quickly I got an incredible helpful response after 20 min. I apologize if I made it look like I didn't appreciate the help, but know that I am deeply thankful.
I will mark this as 'solved' and of course upvote PyTony right now.

Thanks again, I will remember this next time!

abders
Newbie Poster
17 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: