0

I am new in programming. Just bought a book for beginners in Python. In it I got this code:

name = input("name")
email = input("whats ure email:) 
favoriteband = input("ure fav band") 
outputString = name + "|" email + "|" + favoriteband 
fileName = name + ".txt"
file = open(fileName, "wb") 
file.write (outputString) 
print (outputString , " saved in ", fileName) 
file.close ()

According to book its fine but I got this error:

TypeError: a bytes-like object is required, not 'str'

I got no clue how to fix it and book isn't explaining this as well.

4
  • 4
    Add the closing quotes > email = input("whats ure email:") Commented Feb 27, 2019 at 14:02
  • Notice the chromacoding in the snippet you've pasted. It very clearly indicates that you have unbalanced quotes. That said, examine the argument in file.write(outputString). Low level file APIs generally want byte arrays. To use strings, one often needs to wrap the file stream in something higher level, like a print writer. Commented Feb 27, 2019 at 14:08
  • sorry i mispelled this line it is: email = input("whats ure email") Commented Feb 27, 2019 at 14:09
  • 1
    Possible duplicate of TypeError: a bytes-like object is required, not 'str' when writing to a file in Python3 Commented Feb 27, 2019 at 14:20

2 Answers 2

1

Let's go through this:

name = input("Your name: ")
email = input("Your email: ")

The close quotes are needed as has been pointed out.

outputString = name + "|" + email + "|" +  favoriteband 

outputString was missing a + before email

Finally, we need to rewrite you file management:

with open(fileName, "a") as file:
  file.write (outputString) 
  print (outputString , " saved in ", fileName) 

Writing this as a with statement guarantees it will close. Using open(..., "a") opens the file in "append" mode and lets you write multiple strings to a file of the same name.

Finally, if I can editorialize, I am not a fan of this book so far.

Edit: here is the whole code with fixes, in hopes of getting you there.

name = input("name")
email = input("whats ure email:") 
favoriteband = input("ure fav band") 
outputString = name + "|" + email + "|" +  favoriteband 
fileName = name + ".txt"
with open(fileName, "a") as file:
  file.write (outputString) 
  print (outputString , " saved in ", fileName) 

You can verify it works with:

with open(fileName, "r") as file:
  print(file.read())
Sign up to request clarification or add additional context in comments.

3 Comments

Going by what the OP wants to understand from that code as he is a beginner, why not just convert his outputString to bytes before passing to file.write().
it is not working. It is Programming for Beginners Mark Lassoff. Hardly any code works.
@pio I edited in all my code to hopefully help. If it isn't working with my edits then what is the new error?
0

I did some editing (closing quotes and a missing +):

name = input("name:")
email = input("whats ure email:")
favoriteband = input("ure fav band:")

outputString = name + " | " + email + " | " + favoriteband 
fileName = name + ".txt"
file = open(fileName, "w") #opened in write mode but not in binary
file.write (outputString) 
print (outputString , " saved in ", fileName) 
file.close()

You're getting that error because you're writing in binary mode, hence the b in wb for

file = open(fileName, "wb")

Try this instead :

file = open(fileName, "w")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.