我有一个文件夹,其中包含不同类型的文件。我只需要处理图像文件(jpg,png,jpeg,JPG,...)。为此,我使用glob.glob
仅获取带有'jpg','png',...扩展名的文件。
问题是我必须多次使用glob.glob
(与图像扩展名一样多)来获取所有图像:
import glob
images = glob.glob('tests/*.jpg') + glob.glob('tests/*.jpeg') + glob.glob('tests/*.png') + glob.glob('tests/*.JPG')
有没有一种以更优化的形式(例如glob.glob
之类的方式使用glob.glob('path/*.{'jpg', 'png', 'jpeg', 'JPG'}
的方法,还是有一种更简单的函数代替glob
来使用它?
答案 0 :(得分:1)
使用pathlib:
from pathlib import Path
extensions = ['.jpg', '.png', '.jpeg']
images = [x for x in Path('tests').iterdir() if x.suffix.lower() in extensions]
答案 1 :(得分:1)
您不能进行<video data-v-6c3a6820="" id="player" width="1366" height="768" crossorigin="anonymus" class="video-js vjs-default-skin vjs-big-play-centered" data-viblast-key="d6293ad219a4af6550b20f07e66451d72d53c9a3ca77413f2eb2bd3c0b947827df882f2913d340fb42195b89764dd8db49ffb3ba29d41708" data-viblast-src="https://sbb-bg-ku-h1-35.ug-be.cdn.united.cloud/stream?i=dBYjXstwR9iFl6QFDTk5RA&a=4IfyeulCQOI0ox6iM05xTU7I77AJV196e3vMhwvZao19ijsvI0wc_g8lAYTyZuZYqiRxSiyFAntLl91PHmdZsc9NJVntuPghZ5s03ktRfMlpV7Gv3YPbDXNz-DKEMMems9PkKqRKugkDdnj8bcZSCcpMSWFyYCyXkI_ZIpikT6reNFualp09uiT5dQQlbCKd1C8mWxOAo6tF9pTYVC2hVckzX4cni5rj2-Kh7taIT7zAOEHIiRW4GnldTY83w5aOxMpSfY00pD9SX0xmyxMyW_9HUsecpBSX3s1kt-v9ekiQddiO2D98DwklA3OVLkDlE36YNLr1zoQR21ilMLes4jz70Ti9ycUJs4jaOE2w9kuKzixbx6_oH5nqXsebppWc&sp=sbb&u=2kfu9hq2ibxxyxr&player=m3u8v&session=ebcae820-2cd6-45be-92ef-1e5b6c6ee6a5" src="blob:https://eon.tv/01051a85-367f-4ca0-9b53-03c615f8436e"><p data-v-6c3a6820="" class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a data-v-6c3a6820="" href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p></video>
这样的复杂通配符,因为,
如果您查看源代码,将会看到
glob.glob('*.{JPG, png..})
然后,如果您找到def glob(pathname):
"""Return a list of paths matching a pathname pattern.
....
"""
return list(iglob(pathname))
的来源,那么您将看到
iglob
因此,您只能使用def iglob(pathname):
....
....
dirname, basename = os.path.split(pathname)
# voila, here, our complex glob wildcard will certainly break, and can't be used :)
进行简单的glob
bing:)