Finding files using Python



OS is not outside shney library in Python. So I feel like this is the easiest and best way to do it.

This article is provided by Sumit Kumar . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See your article appearing on the Python.Engineering homepage and help other geeks.


# Python code to find .mp3 files in the current
# folder (We can change the file type / name and the path
# as required.

import os

 
# This is to get the directory that program
# currently running

dir_path = os.path.dirname (os.path.realpath (__ file__))

 

for root, dirs, files in os.walk (dir_path):

  for file in files : 

 

# change extension from .mp3 to

  # one of your choice.

if file . endswith ( `.mp3` ):

print root + `/` + str ( file )