Downloading Mp3 using Python on Windows controls the song, however on Linux it doesn't

I installed a script to download mp3 using urllib2 in Python.

url = 'example.com' req2 = urllib2.Request(url) response = urllib2.urlopen(req2) #grab the data data = response.read() mp3Name = "song.mp3" song = open(mp3Name, "w") song.write(data) # was data2 song.close() 

It turns out that this is due to the fact that I'm loading it into Windows or my current version of Python. I tested the code on my Ubuntu distribution and the mp3 file loaded perfectly fine ... So I just used the simple urllib2.openurl method and it worked perfectly!

Summarizing:

  • I am using urllib2.openurl in Python in a Ubuntu distribution.
  • I am using a newer version of Python, but I feel that this cannot be.
  • MP3 encoded in LAME.

Does anyone know what caused a weird problem running code on my windows box? I wonder why loading on Windows distorted mp3?

+3
source share
1 answer

Try binary mode. open(mp3Name, "wb") probably get a line open(mp3Name, "wb") .

The file is binary, yes. This is a regime that was not there. When a file is opened, it can be configured as a text file (this is the default). When he does, he will convert the line endings to the platform. On Windows, line endings \r\n In most other places, this is either \r or \n . This change spoiled the data stream.

+13
source

Source: https://habr.com/ru/post/1495095/


All Articles