Windows 上的二进制文件有什么用? [英] What's with binary files on Windows?

查看:47
本文介绍了Windows 上的二进制文件有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个下载文件的脚本,但是当我下载二进制可执行文件、swf、图像等时,它只能在 Unix/Linux/OSX 上运行

I made a script to download a file, but it only works on Unix/Linux/OSX when I'm downloading binary executables, swf's, images, etc

\#Modfied section from PWB.py  
import sys  
if sys.version_info<(2,8):  
    import urllib as request  
else:  
    import urllib.request as request  
x=request.urlopen("http://homestarrunner.com/intro.swf")  
y=x.read()  
x.close()  
z=open("intro.swf","w")  
z.write(y)  
z.close() 

我会得到文件,以及文件中通常无法读取的垃圾,但它是无法读取的.

I will get the the file, and the usual unreadable garbage in the file, but it will be unreadable.

似乎二进制文件在 Windows 上总是有这些问题.这是为什么?

It seems binary files always have these sorts of problem on Windows. Why is this?

附注.我如何编写我的 python 代码以便下载?

PS. How could I write my python code so that it will download?

推荐答案

来自 Python 2 文档:

在 Windows 上,'b' 附加到模式以二进制模式打开文件,所以还有像rb"、wb"这样的模式,和'r + b'.Windows 上的 Python 使文本和二进制的区别档案;行尾字符文本文件会自动更改读取或写入数据时略有变化.这种幕后修改文件数据适用于 ASCII 文本文件,但它会破坏二进制数据就像在 JPEG 或 EXE 文件中那样.是使用二进制模式时要非常小心读取和写入此类文件.在Unix,附加一个b"并没有什么坏处到模式,所以你可以使用它平台无关的所有二进制文件文件.

On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.

这篇关于Windows 上的二进制文件有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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