Python文件IO'w'与'wb' [英] Python file IO 'w' vs 'wb'

查看:131
本文介绍了Python文件IO'w'与'wb'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道从Python编写文件时真正的区别是什么.从我看到的是使用w还是wb的文本中,我得到的都是相同的结果.

Wondering what the real difference is when writing files from Python. From what I can see if I use w or wb I am getting the same result with text.

我认为另存为二进制文件会在十六进制编辑器中仅显示二进制值,但同时还会显示文本,然后显示该文本的ASCII版本.

I thought that saving as a binary file would show only binary values in a hex editor, but it also shows text and then ASCII version of that text.

保存文本时,两者可以互换使用吗? (Windows用户)

Can both be used interchangably when saving text? (Windows User)

推荐答案

仅在Windows中,在后一种情况下,.write('\n')写入一个字节,值为10.值13和10.

Only in Windows, in the latter case, .write('\n') writes one byte with a value of 10. In the former case, it writes two bytes, with the values 13 and 10.

您可以通过查看生成的文件大小并在十六进制编辑器中检查文件来向自己证明这一点.

You can prove this to yourself by looking at the resulting file size, and examining the files in a hex editor.

在与POSIX相关的操作系统(UNIX,SunOS,MacOS,Linux等)中,'w''wb'之间没有区别.

In POSIX-related operating systems (UNIX, SunOS, MacOS, Linux, etc.), there is no difference beetween 'w' and 'wb'.

这篇关于Python文件IO'w'与'wb'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆