代码如下:
import os
import fnmatch
def getAllJpgPng(path):
# 返回的是所有jpg和png图片的名称列表
res = []
for folderName, subFolders,fileNames in os.walk(path):
for filename in fileNames:
if fnmatch.fnmatch(filename, '*.jpg') or fnmatch.fnmatch(filename, '*.png'):
res.append(filename)
return res
jpgs = getAllJpgPng('/Users/trent2766/Pictures/工作照片')
print(jpgs)
- 更多详细的匹配可以学习正则表达式的用法,使用
fnmatch.fnmatch()
。 - 如果想要复制到其他文件夹中,可以使用
shutil.copy()
- 如果想要删除这些图片,使用
os.unlink(path)
参考文献
- https://github.com/datawhalechina/team-learning-program/tree/master/OfficeAutomation