Python Forum
Why the file is not downloaded in full with this script?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why the file is not downloaded in full with this script?
#1
Hello,

I use python3 and this little script next
import requests

url = 'http://mp3-red.co/stream/44760541/oscar-peterson-night-and-day.mp3'
r = requests.get (url, allow_redirects = True)
open ('oscar-peterson-night-and-day.mp3', 'wb'). write (r.content)
should allow the download of the file by giving the name of the url included in the py.
The file is downloading but not entirely!
Why?

Thank you for your help
Envoyer des commentaires
Historique
Enregistré
Communauté
Reply
#2
Is there any .mp3 on that terrible "illegal" site that work Dodgy

Back to Python,this is how you download.
import requests

url = 'http://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3'
with open('sample.mp3', 'wb') as f:
    f.write(requests.get(url).content)
Get file name from url.
import requests
import os

url = 'http://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3'
file_name = os.path.basename(url)
with open(file_name, 'wb') as f:
    f.write(requests.get(url).content)
Reply
#3
You should always close the file, after you're done with writing.
The context manager does it for you. After the interpreter leaves the with-block,
it closes the file. Only when the file is closed, it's guaranteed, that all buffers are written.
It is explained here: http://blog.lerner.co.il/dont-use-python...r-depends/

If you don't use a context manager, you've to close the file explicit.
fd = open(.....)
fd.write('something')
fd.close()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Downloaded file corrupted emont 5 821 Oct-01-2023, 11:32 AM
Last Post: snippsat
  I downloaded a script and I cant run this luoxr18 3 1,195 Apr-16-2023, 09:33 AM
Last Post: Larz60+
  How to make a test data file for the full length of definition? MDRI 6 3,539 Apr-16-2021, 01:47 AM
Last Post: MDRI
  How to create local copies of Python packages so they do not have to be downloaded okhajut 3 2,009 Sep-29-2020, 02:22 PM
Last Post: buran
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,133 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Execute full script samuelbachorik 0 1,538 Aug-06-2020, 08:09 PM
Last Post: samuelbachorik
  How to append a tuple full of records to a dbf file in Python? DarkCoder2020 4 3,723 May-29-2020, 02:40 PM
Last Post: DarkCoder2020
  How to create an environment for program downloaded from github StartedNewLife 3 2,172 Jan-25-2020, 06:34 PM
Last Post: ibreeden
  How to get file name without the full path details and without extension aruncom2006 1 5,913 Jan-13-2020, 07:37 AM
Last Post: Larz60+
  How to search full path of specified file SriRajesh 3 2,394 Dec-14-2019, 04:52 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020