(Feb-01-2019, 09:47 AM)DreamingInsanity Wrote: he only part that wasnt included in the code was that once it found what ending the url had, it would call a function and it would download the file from that url. I needed the ending so I could save it with that specific ending.
That's great example that one should be clear what ultimate goal is.
Instead of checking extensions one by one, you should use proper tools to extract file extension from url
import os import pathlib url1 = 'https://www.somedomain.com?download=somefile.div' url2 = 'https://www.somedomain.com/somefile.divf' #using os.path.splitext for url in (url1, url2): ext = os.path.splitext(url)[-1] local_path = f'c:/somefolder/localfile{ext}' print(f'{url} --> {local_path}') print() # using pathlib for url in (url1, url2): ext = pathlib.Path(url).suffix local_path = f'c:/somefolder/localfile{ext}' print(f'{url} --> {local_path}')
Output:https://www.somedomain.com?download=somefile.div --> c:/somefolder/localfile.div
https://www.somedomain.com/somefile.divf --> c:/somefolder/localfile.divf
https://www.somedomain.com?download=somefile.div --> c:/somefolder/localfile.div
https://www.somedomain.com/somefile.divf --> c:/somefolder/localfile.divf
>>>
EDIT: I just realised that you may have a case when it url does not end in desired extension.In this case you may want to check that extension is in predefined list of "eligible" extemsions.
Also instead of using os and pathlib module you can split url string at
'.'
, take the last element and check if it is in the desired list of extensions.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs