有以下网址。
original = 'https://dev.s3.amazonaws.com/production/uploads/2017/11/filename.jpg'
我只想从中提取/production/uploads/2017/11
可以只提取文件名并提取包含文件名的路径,但是请告诉我是否有一种很好的方法来提取除文件名之外的路径。
original_image_name = original.split('/')[-1] # 'filename.jpg'
from urllib.parse import urlparse
original_image_url = urlparse(original)
original_image_path = original_image_url.path # '/production/uploads/2017/11/filename.jpg'
答案 0 :(得分:0)
快速的方法是使用regular expressions,就像这样:
(http[s]?:\/\/)?([^\/\s]+)(.*)
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以使用:
import os
os.path.dirname(original_image_path)
答案 3 :(得分:0)
您将获得预期的输出
original = "https://dev.s3.amazonaws.com/production/uploads/2017/11/filename.jpg"
test = original.split("/")
test = test[3:]
test = test[:-1]
sd = '/'.join(test)
print sd
输出
production/uploads/2017/11