How to extract a filename from a URL & append a word to it?

时间:2021-09-05 21:19:44

I have the following url:

我有以下网址:

url = http://photographs.500px.com/kyle/09-09-201315-47-571378756077.jpg

url = http://photographs.500px.com/kyle/09-09-201315-47-571378756077.jpg

I would like to extract the file name in this url: 09-09-201315-47-571378756077.jpg

我想在这个网址中提取文件名:09-09-201315-47-571378756077.jpg

Once I get this file name, I'm going to save it with this name to the Desktop.

获得此文件名后,我将使用此名称将其保存到桌面。

filename = **extracted file name from the url**     
download_photo = urllib.urlretrieve(url, "/home/ubuntu/Desktop/%s.jpg" % (filename))

After this, I'm going to resize the photo, once that is done, I've going to save the resized version and append the word "_small" to the end of the filename.

在此之后,我将调整照片的大小,一旦完成,我将保存调整大小的版本并在文件名的末尾附加单词“_small”。

downloadedphoto = Image.open("/home/ubuntu/Desktop/%s.jpg" % (filename))               
resize_downloadedphoto = downloadedphoto.resize.((300, 300), Image.ANTIALIAS)
resize_downloadedphoto.save("/home/ubuntu/Desktop/%s.jpg" % (filename + _small))

From this, what I am trying to achieve is to get two files, the original photo with the original name, then the resized photo with the modified name. Like so:

从这个,我想要实现的是获得两个文件,原始照片与原始名称,然后调整大小的照片与修改名称。像这样:

09-09-201315-47-571378756077.jpg

09-09-201315-47-571378756077.jpg

09-09-201315-47-571378756077_small.jpg

09-09-201315-47-571378756077_small.jpg

How can I go about doing this?

我该怎么做呢?

5 个解决方案

#1


3  

filename = url[url.rfind("/")+1:]
filename_small = filename.replace(".", "_small.")

maybe use ".jpg" in the last case since a . can also be in the filename.

也许在最后一种情况下使用“.jpg”,因为a。也可以在文件名中。

#2


43  

You can use urlparse and os.path built-in python modules. Example:

您可以使用urlparse和os.path内置的python模块。例:

>>> import urlparse, os
>>> url = "http://photographs.500px.com/kyle/09-09-201315-47-571378756077.jpg"
>>> a = urlparse.urlparse(url)
>>> a.path
'/kyle/09-09-201315-47-571378756077.jpg'
>>> os.path.basename(a.path)
'09-09-201315-47-571378756077.jpg'

If you have trouble importing urlparse, try this:

如果您在导入urlparse时遇到问题,请尝试以下操作:

Python 2

Python 2

from six.moves.urllib.parse import urlparse

Python 3

Python 3

from urllib.parse import urlparse

#3


5  

You could just split the url by "/" and retrieve the last member of the list:

您可以将URL拆分为“/”并检索列表的最后一个成员:

    url = "http://photographs.500px.com/kyle/09-09-201315-47-571378756077.jpg"
    filename = url.split("/")[-1] 
    #09-09-201315-47-571378756077.jpg

Then use replace to change the ending:

然后使用replace来改变结尾:

    small_jpg = filename.replace(".jpg", "_small.jpg")
    #09-09-201315-47-571378756077_small.jpg

#4


1  

Python split url to find image name and extension

Python拆分网址以查找图像名称和扩展名

helps you to extract the image name. to append name :

帮助您提取图像名称。附加名称:

imageName =  '09-09-201315-47-571378756077'

new_name = '{0}_small.jpg'.format(imageName) 

#5


1  

os.path.basename(url)

os.path.basename(URL)

Why try harder?

为什么要努力?

In [1]: os.path.basename("https://foo.com/bar.html")
Out[1]: 'bar.html'

In [2]: os.path.basename("https://foo.com/bar")
Out[2]: 'bar'

In [3]: os.path.basename("https://foo.com/")
Out[3]: ''

In [4]: os.path.basename("https://foo.com")
Out[4]: 'foo.com'

#1


3  

filename = url[url.rfind("/")+1:]
filename_small = filename.replace(".", "_small.")

maybe use ".jpg" in the last case since a . can also be in the filename.

也许在最后一种情况下使用“.jpg”,因为a。也可以在文件名中。

#2


43  

You can use urlparse and os.path built-in python modules. Example:

您可以使用urlparse和os.path内置的python模块。例:

>>> import urlparse, os
>>> url = "http://photographs.500px.com/kyle/09-09-201315-47-571378756077.jpg"
>>> a = urlparse.urlparse(url)
>>> a.path
'/kyle/09-09-201315-47-571378756077.jpg'
>>> os.path.basename(a.path)
'09-09-201315-47-571378756077.jpg'

If you have trouble importing urlparse, try this:

如果您在导入urlparse时遇到问题,请尝试以下操作:

Python 2

Python 2

from six.moves.urllib.parse import urlparse

Python 3

Python 3

from urllib.parse import urlparse

#3


5  

You could just split the url by "/" and retrieve the last member of the list:

您可以将URL拆分为“/”并检索列表的最后一个成员:

    url = "http://photographs.500px.com/kyle/09-09-201315-47-571378756077.jpg"
    filename = url.split("/")[-1] 
    #09-09-201315-47-571378756077.jpg

Then use replace to change the ending:

然后使用replace来改变结尾:

    small_jpg = filename.replace(".jpg", "_small.jpg")
    #09-09-201315-47-571378756077_small.jpg

#4


1  

Python split url to find image name and extension

Python拆分网址以查找图像名称和扩展名

helps you to extract the image name. to append name :

帮助您提取图像名称。附加名称:

imageName =  '09-09-201315-47-571378756077'

new_name = '{0}_small.jpg'.format(imageName) 

#5


1  

os.path.basename(url)

os.path.basename(URL)

Why try harder?

为什么要努力?

In [1]: os.path.basename("https://foo.com/bar.html")
Out[1]: 'bar.html'

In [2]: os.path.basename("https://foo.com/bar")
Out[2]: 'bar'

In [3]: os.path.basename("https://foo.com/")
Out[3]: ''

In [4]: os.path.basename("https://foo.com")
Out[4]: 'foo.com'