Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting Extension
#1
Hey! Friends.
Problem: How to get extension of the file from url or else anything using request or any module?
Another Problem: Can I Use To Split Files In Python And Download Them Using Threading?
Here Is The Code:
import requests
import threading
def download(url):
    r = requests.get(url, allow_redirects=True)

    response = requests.get(url, stream = True)

    text_file = open("data.html","wb")
    for chunk in response.iter_content(chunk_size=1024):
        text_file.write(chunk)
    #writing one chunk at a time to text file


    text_file.close()
t = threading.Thread(target=download,args=('https://youtu.be/zcywDfl7vJs',))
t.start()
Reply
#2
What do you mean? Typically, URLs these days don't use file extensions as they often aren't pointing to files. Describe what you're trying to do at a high level.
Reply
#3
I Am Trying To Make A Download Manager But Problem Is How to get file extension from anything like headers,url,etc
Reply
#4
Perhaps use the Content-Type header if it's set?

Please also don't write in title case; it's quite difficult to read.
Reply
#5
import requests
import youtube_dl
r = requests.get("https://www.google.com")
from mimetypes import guess_extension
print(guess_extension(r.headers['content-type'].partition(';')[0].strip()))
Can Above Code WOrk?
Reply
#6
Have you tried it?
Reply
#7
Yes! It Worked For Me/
Reply


Forum Jump:

User Panel Messages

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