Windows 上的二进制文件是怎么回事?
我编写了一个脚本来下载文件,但它仅适用于 Unix/Linux/OSX,当我下载二进制可执行文件、swf、图像等时,
\#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()
我会得到该文件,以及文件中通常不可读的垃圾,但它将无法读取。
看来二进制文件在 Windows 上总是存在此类问题。 这是为什么呢?
附言。我怎样才能编写我的Python代码以便它可以下载?
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.
It seems binary files always have these sorts of problem on Windows.
Why is this?
PS. How could I write my python code so that it will download?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以二进制模式打开二进制文件。
Open binary files in binary mode.
来自 Python 2 文档:
From the Python 2 documentation:
在 Windows 上使用
z=open("intro.swf","wb")
以二进制模式打开文件。http://docs.python.org/tutorial/inputoutput.html
Use
z=open("intro.swf","wb")
on Windows to open the file in binary mode.http://docs.python.org/tutorial/inputoutput.html
您必须在 open() 的参数中使用“wb”才能以二进制模式获取它 - 默认为文本模式,它将 \n 转换为 CR/LF。
You have to use "wb" in the argument for open() to get it in binary mode - the default is text mode which converts \n to CR/LF.