Python Forum

Full Version: Getting Extension
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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.
I Am Trying To Make A Download Manager But Problem Is How to get file extension from anything like headers,url,etc
Perhaps use the Content-Type header if it's set?

Please also don't write in title case; it's quite difficult to read.
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?
Have you tried it?
Yes! It Worked For Me/