myBytesVar = b'line1\nline2'
with open('myFile.txt', 'wb') as fw:
fw.write(myBytesVar)
with open('myFile.txt', 'r') as fr:
myVar = fr.read()
print(myVar)
The default mode is 'r' (open for reading text, synonym of 'rt'). For
binary read-write access, the mode 'w+b' opens and truncates the file
to 0 bytes. 'r+b' opens the file without truncation.
如果文件的内容已存储在
myBytesVar
中,则无需重新读取该文件:Python的编码假设在没有显式编码的情况下以文本形式读取文件是platform-dependent,所以我假设UTF-8可以工作。你知道吗
如果你想用一个“with”:当你写它的时候“wb”是好的。 当你读文件的时候,试试看
以下是我们应该使用的模式的一些信息:
在这里阅读更多。 https://docs.python.org/3/library/functions.html#open
相关问题 更多 >
编程相关推荐