I have a Question. I'm a little bit confuse in it. I have URL from which images and videos are coming.I want to get the name from that link and check it is it an image or video and perform action on it. like
if filename.endwith(.jpg or .png):
print("it's an image")
else filename.endwith(.mp4, .avi):
print("it's a video)
the filename is a list. in which all the data are store:
ls = ["C:/Users/OB/Desktop/DevoMech Project/6.JPG" , "C:/Users/OB/Desktop/DevoMech Project/7.JPG"]
if ls.endwith(.JPG or .JPEG):
item1 = QtWidgets.QListWidgetItem(QIcon(ls), "item1")
self.dlistWidget.addItem(item1)
#item2 = QtWidgets.QListWidgetItem(QIcon("C:/Users/OB/Desktop/DevoMech Project/7.JPEG"), "item2")
self.dlistWidget.addItem(item2)
and instead of item one the actual name display.
I always prefer to use the os builtin if possible for paths and also capture unsupported types
import os
video_types = ('.mp4', '.avi', '.jpeg')
image_types = ('.png', '.jpg')
filenames = ["/test/1.jpg","/test/2.avi","/test/unknown.xml","/test/noextention"]
for filename in filenames:
print(filename)
if os.path.splitext(filename)[1] in video_types:
print("Its a Video")
elif os.path.splitext(filename)[1] in image_types:
print("Its an Image")
else:
print("No Idea")
/test/1.jpg
Its an Image
/test/2.avi
Its a Video
/test/unknown.xml
No Idea
/test/noextention
No Idea
from urlparse import urlparse
from os.path import splitext
url = "sample/test/image.png"
image_formats = [".png", ".jpeg"]
video_formats = [".mp4", ".mp3", ".avi"]
def get_ext(url):
"""Return the filename extension from url, or ''."""
parsed = urlparse(url)
root, ext = splitext(parsed.path)
return ext
if get_ext(url) in image_formats:
print("it's an image")
elif get_ext(url) in video_formats:
print("it's a video")
else:
print("some differnet format")
it will work for any type of url
www.example.com/image.jpg
https://www.example.com/page.html?foo=1&bar=2#fragment
https://www.example.com/resource
if filename.lower().endswith(('.png', '.jpg', '.jpeg')): print("it's an image") elif filename.lower().endswith(('.mp4', '.avi')): print("it's a video")
You can use String inbuilt split method. For ex:-
file_name = 'my_image.jpg'
file_type = file_name.split('.')[-1] # it will give you 'jpg' last element
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.