1.1K
In this example we will list all the files in a specific directory that have a certain file extension
This example shows how to find files in a directory with a specific extension using the listdir() function and the endswith() function.
Example
We will search for all .py files
import os # Specifies the path in path variable path="C:\Python38-32\List examples" for i in os.listdir(path): # List all files with .py if i.endswith(".py"): print("Files ending with .py are:",i)
When you run this you will see something like this
>>> %Run fileextensions.py Files ending with .py are: add2lists1.py Files ending with .py are: add2lists2.py Files ending with .py are: add2lists3.py Files ending with .py are: averagelist1.py Files ending with .py are: lengthlist.py Files ending with .py are: lengthlist1.py Files ending with .py are: lengthlist2.py Files ending with .py are: listcountposneg1.py Files ending with .py are: listcountposneg2.py Files ending with .py are: listoddeven1.py Files ending with .py are: listoddeven2.py Files ending with .py are: oddeveninlists.py Files ending with .py are: oddeveninlists1.py Files ending with .py are: smalllargelist1.py Files ending with .py are: smalllargelist2.py