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.

Vasilisa
  • 4,604
  • 3
  • 20
  • 25
pio
  • 13
  • 2
  • 4
    Add the closing quotes > `email = input("whats ure email:")` – vahdet Feb 27 '19 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. – F. P. Freely Feb 27 '19 at 14:08
  • sorry i mispelled this line it is: email = input("whats ure email") – pio Feb 27 '19 at 14:09
  • 1
    Possible duplicate of [TypeError: a bytes-like object is required, not 'str' when writing to a file in Python3](https://stackoverflow.com/questions/33054527/typeerror-a-bytes-like-object-is-required-not-str-when-writing-to-a-file-in) – 001 Feb 27 '19 at 14:20

2 Answers2

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())
Charles Landau
  • 4,187
  • 1
  • 8
  • 24
  • 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()`. – faruk13 Feb 27 '19 at 14:14
  • it is not working. It is Programming for Beginners Mark Lassoff. Hardly any code works. – pio Feb 27 '19 at 14:15
  • @pio I edited in all my code to hopefully help. If it isn't working with my edits then what is the new error? – Charles Landau Feb 27 '19 at 14:23
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")

faruk13
  • 1,276
  • 1
  • 16
  • 23
merieme
  • 191
  • 7