function
Get the file of the folder + get the file of the specific format in the folder, such as.png, etc
The program
import os
folder = r"D:\python\untitled1"
files = os.listdir(folder)
print(files)
def file_filter(f):
if f[-4:] in ['.jpg', '.png']:
return True
else:
return False
files = list(filter(file_filter, files))
print(files)
Copy the code
The effect
The folder you want to view
To run the program
As you can see, first print the file names for all formats, then print the file names for the formats we locked.