I need to save a file with the name of the given acquisition path's file.
Given an URL I would like to parse it and extract the name of the file, here's my code...
I read a JSON parameter and give it to the Parse Url function. The acquisition path is a string.
ParseUrl.py:
from urllib.parse import urlparse as up
a = up(jtp["AcquisitionPath"]) # => http://127.0.0.1:8000/Users/YodhResearch/Desktop/LongCtrl10min.tiff
print(a)
print(os.path.basename(a))
Result:
ParseResult(scheme='http', netloc='127.0.0.1:8000', path='/Users/YodhResearch/Desktop/LongCtrl10min.tiff', params='', query='', fragment='')
[....]
TypeError: expected str, bytes or os.PathLike object, not ParseResult
As you can see it Parse the URL but "LongCtrl10min.tiff" is not in the fragment section but is all on the path section. Why is that happening? Maybe because "AcquisitionPath" is a string and UrlParse recognize all as a unique path?
EDIT:
a.path WORKS, I would like to know why I don't get it into the fragment section.
Here's another example:
from urllib.parse import urlparse as up
string = "http://127.0.0.1:8000/GIULIO%20FERRARI%20FOLDER/Giulio%20_%20CSV/Py%20Script/sparse%20python/tiff_test.tiff_IDAnal#1_IDAcq#10_TEMP_.json"
a = up(string)
print(a)
print(os.path.basename(a))
Results:
ParseResult(scheme='http', netloc='127.0.0.1:8000', path='/GIULIO%20FERRARI%20FOLDER/Giulio%20_%20CSV/Py%20Script/sparse%20python/tiff_test.tiff_IDAnal', params='', query='', fragment='1_IDAcq#10_TEMP_.json')
See, Now it doesn't get the right fragment that should be: "tiff_test.tiff_IDAnal#1_IDAcq#10_TEMP_.json"
SOLUTION:
Fragment needs '#' symbol! Thanks to all.