Python Program to Search py Files in a Folder
In this article, we will be solving a very interesting problem based on searching. According to the question we have to search py files in a folder.
If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.
Question - Write a Python Program to Search py Files in a Folder
Logic and Explanation
Question - Write a Python Program to Search py Files in a Folder
Python Program to Search py Files in a Folder - We have to search py files in a folder let's say in a folder name "Python Programs" which is present in our current working directory.
Now to search py files we will first take out the list of files present in the folder "Python Programs". To take out the list of files we will be using os module. So first let's import the os module in our python file.
import os
To get list of all the files and folder in a directory or folder, we use os.listdir() and we pass the path of the directory in which we have to search for the py file.
os.listdir(path)
Now, there is one problem that we get the list of all files and folder from the above method but we only want to search in files. So, we will first sort the files in this. For this we will use another method of os module i.e. os.path.isfile() to sort out the files and folders. In this function we will pass the path of file or folder which we want to check.
os.path.isfile(path)
So, this was the details of modules and some python os module methods that we will be using to solve this problem.
Now after we have sorted out the names of files from mixture of files and folder, we will know check for py files. For this the logic we will be using is splitting the name string at "." and then checking the second part of the string. If it is equal to "py" then it is a python file and we will print it's name.
I hope the logic is clear to you. You can take look at the code below and if any doubt comes in your mind feel free to ask in the comments below.
import os
# List all files in a directory using os.listdir
path = './Python Programs/'
for file in os.listdir(path):
#Checking for file
if os.path.isfile(os.path.join(path, file)):
#Splitting string at "."
M = file.split(".")
if M[1] == "py":
print(file)
path - Directory of the folder in which we have to search of py file. Here, I have written the relative path of the folder.
M - When we split a string we get a list which is stored in this variable. M is a list with it's second element i.e. element at index 1 is the extension of the file.
Now let's run the file and check our output. You can see the screenshot of the folder "Python Programs" and out put below.
calinpy.py
countdown.py
countrowcsv.py
countupperc.py
countvowel.py
currdirt.py
So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.
For More Such Python Programs - Click Here
Enjoy Coding!!!
Also Read :-
Extract Phone Number Details Using Python
Post a Comment
For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.