我是编程新手。刚在Python
为初学者买了一本书。在里面我有个密码:
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 ()
根据这本书,它很好,但我有一个错误:
TypeError: a bytes-like object is required, not 'str'
我不知道该怎么修,书也没有解释
我做了一些编辑(右引号和缺少的
+
):得到这个错误是因为您是以二进制模式编写的,因此
b
中的wb
file = open(fileName, "wb")
请尝试以下操作:
file = open(fileName, "w")
我们来看看这个:
正如已经指出的那样,我们需要简短的引述
outputString
在email
之前缺少一个+
最后,我们需要重写您的文件管理:
把它写成
with
语句可以保证它会关闭。使用open(..., "a")
以“append”模式打开文件,并允许您向同名文件写入多个字符串最后,如果我能发表社论的话,到目前为止我还不是这本书的粉丝
编辑:这里是完整的代码与修复,希望让你到那里
您可以验证它是否适用于:
相关问题 更多 >
编程相关推荐