File Searching using Python Last Updated : 29 Dec, 2022 Comments Improve Suggest changes Like Article Like Report There may be many instances when you want to search a system.Suppose while writing an mp3 player you may want to have all the '.mp3' files present. Well here's how to do it in a simple way. This code searches all the folders in the file it's being run. If you want some other kinds of files just change the extension. Python3 # Python code to search .mp3 files in current # folder (We can change file type/name and path # according to the requirements. import os # This is to get the directory that the program # is currently running in. dir_path = os.path.dirname(os.path.realpath(__file__)) for root, dirs, files in os.walk(dir_path): for file in files: # change the extension from '.mp3' to # the one of your choice. if file.endswith('.mp3'): print (root+'/'+str(file)) os is not an external library in python. So I feel this is the simplest and the best way to do this. Use the glob module: One alternative approach to searching for files with a specific extension is to use the glob module. This module provides a way to search for files with a specific pattern using the glob function. For example, to search for all .txt files in the current directory, you could use the following code: Python3 import glob files = glob.glob('*.mp3') for file in files: print(file) The glob function returns a list of file paths that match the specified pattern. In this case, the pattern '*.mp3' matches all files in the current directory that have the .mp3 extension. You can also specify a different directory to search in by including the path in the pattern. For example, 'path/to/directory/*.mp3' would search for all .mp3 files in the path/to/directory directory. The glob module also supports more advanced patterns, such as '*.mp3' to match all files with the .mp3 extension in any directory, or '**/*.mp3' to match all .mp3 files in all subdirectories, recursively. You can learn more about the different pattern options in the documentation for the glob module. Comment More infoAdvertise with us Next Article Read File As String in Python S soumith Improve Article Tags : Misc Python python-utility Practice Tags : Miscpython Similar Reads Read File As String in Python Python provides several ways to read the contents of a file as a string, allowing developers to handle text data with ease. In this article, we will explore four different approaches to achieve this task. Each approach has its advantages and uses cases, so let's delve into them one by one. Read File 3 min read Read File As String in Python Python provides several ways to read the contents of a file as a string, allowing developers to handle text data with ease. In this article, we will explore four different approaches to achieve this task. Each approach has its advantages and uses cases, so let's delve into them one by one. Read File 3 min read Writing to file in Python Writing to a file in Python means saving data generated by your program into a file on your system. This article will cover the how to write to files in Python in detail.Creating a FileCreating a file is the first step before writing data to it. In Python, we can create a file using the following th 4 min read Run Python File In Vscode Visual Studio Code (VSCode) is a popular and versatile code editor that supports Python development with various features and extensions. In this article, we will see how to run Python files in VsCode.Below is the step-by-step procedure by which we can run the basic Python Script in VScode:Step 1: I 2 min read Reading and Writing to text files in Python Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). Text files: In this type of file, Each line of text is terminated with a special character called EOL 8 min read Reading and Writing lists to a file in Python Reading and writing files is an important functionality in every programming language. Almost every application involves writing and reading operations to and from a file. To enable the reading and writing of files programming languages provide File I/O libraries with inbuilt methods that allow the 5 min read Like