我有一个返回字符串的函数.该字符串包含回车符和新换行符(0x0D,0x0A).但是,当我写入文件时,它只包含新的换行符.有没有办法让输出包括回车和新换行.
msg = function(arg1, arg2, arg3)
f = open('/tmp/output', 'w')
f.write(msg)
f.close()
Run Code Online (Sandbox Code Playgroud)
Ign*_*ams 198
如果要写字节,则应以二进制模式打开文件.
f = open('/tmp/output', 'wb')
Run Code Online (Sandbox Code Playgroud)
Eti*_*eni 31
这只是一个“更干净”的版本with:
with open(filename, 'wb') as f:
f.write(filebytes)
Run Code Online (Sandbox Code Playgroud)
yay*_*aya 11
写入字节并创建文件(如果不存在):
f = open('./put/your/path/here.png', 'wb')
f.write(data)
f.close()
Run Code Online (Sandbox Code Playgroud)
wb表示以write binary模式打开文件。
| 归档时间: |
|
| 查看次数: |
145221 次 |
| 最近记录: |