What's with binary files on Windows?
I made a scri开发者_运维技巧pt 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?
Open binary files in binary mode.
z = open("intro.swf","wb")
From the Python 2 documentation:
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.
Use z=open("intro.swf","wb")
on Windows to open the file in binary mode.
http://docs.python.org/tutorial/inputoutput.html
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.
精彩评论