[英]Python how to write bytes in a .txt file
I have a program that puts your string into bytes, and writes it on a new line every time.我有一个程序可以将您的字符串放入字节中,并每次都将其写在新行上。 For some reason, it doesn't do that.出于某种原因,它不会那样做。 It gives me:它给了我:
File "c:/InstaBots/password.py", line 44, in store
file.write(encrypted_data + '\n')
TypeError: can't concat str to bytes
Here is the code:这是代码:
with open("passwords.txt", "rb") as file:
file_data = file.read()
encrypted_data = f.encrypt(file_data)
decoded = encrypted_data.decode()
print(encrypted_data)
with open("passwords.txt", "wb") as file:
file.write(encrypted_data + '\n')
Any ideas why it won't convert?任何想法为什么它不会转换? I took out the '/n', assuming it would be that, but then it rewrites over itself everytime I run the program.我取出了'/n',假设它是这样,但是每次我运行程序时它都会重写它自己。 If that is the problem, how would I write bytes on a new line?如果这是问题所在,我将如何在新行上写入字节?
您需要将\\n
转换为字节。
file.write(encrypted_data + bytes('\n'))
Your code in the end should be :你的代码最终应该是:
with open("passwords.txt", "ab") as file:
file.write(b'\n'+encrypted_data + b'\n')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.