2

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 expected 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.

Nik Reiman
  • 39,067
  • 29
  • 104
  • 160
user830904
  • 21
  • 1
  • Hint "text mode" vs "binary mode" on systems that care. –  Jul 06 '11 at 05:47
  • See also [0A hex bug on writing to file?](http://stackoverflow.com/questions/4154369/0a-hex-bug-on-writing-to-file). (Same underlying problem, but to see that, one needs to know the answer, so I'm not closing as duplicate.) – Paŭlo Ebermann Jul 06 '11 at 12:54

1 Answers1

4

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

open("this_is_file_two.mid", "wb").write(g)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358