开发者

If I use urlretrive and urlopen to access the same url, I end up with different files. Why?

I'm fairly new to python, (and programming in general), and I've ran into some trouble while writing a program to fetch midi files from the internet. Below is some code that I exp开发者_Python百科ected to write two identical files:

#method one
url = "http://.../asfd.mid"
urllib.urlretrieve(url, "C:\...\this_is_file_one.mid")

#method two
g = urllib2.urlopen(url).read()
open("this_is_file_two.mid", "w").write(g)

Method one produces a valid midi file, while method two does not. When I compared the two files in a hex editor, I found that the invalid file, (file_two), had an extra byte, '0D', inserted before each '0A' that occurred in the valid file. I did a little copy-paste-comparing to see if there were any other differences, but nothing popped up. There could easy have been more differences that I didn't find, though. The same problem happens with a different url.

I'm stumped. Any illumination would be greatly appreciated.

Thanks.


urllib.urlretrieve() opens the file to save in binary mode.

open("this_is_file_two.mid", "wb").write(g)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜