request - Python 2/3: Get name and extension of file at URL -
when download file: https://drive.google.com/uc?export=download&id=0b4ifintpkesatwzxwjeyd1fsrg8
chrome knows named testzip2.zip
, downloads download folder name.
how can name in python (in way works in both python 2.7 , 3.x)?
my previous approach:
response = urlopen(url) header = response.headers['content-disposition'] original_file_name = next(x x in header.split(';') if x.startswith('filename')).split('=')[-1].lstrip('"\'').rstrip('"\'')
seems not work reliably - , randomly fails keyerror: 'content-disposition'
, or attributeerror: 'nonetype' object has no attribute 'split'
you can use
import re ... content_disposition = response.headers.get('content-disposition') match = re.findall(r'filename="([\w\d\.]+)"', content_disposition) filename = match[0]
however in python 3, there handy method on httpmessage
object filename.
filename = response.headers.get_filename() # python3
Comments
Post a Comment