File Searching using Python Last Updated : 29 Dec, 2022 Summarize Comments Improve Suggest changes Share 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 Writing to file 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 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 Reading and Writing XML Files in Python Extensible Markup Language, commonly known as XML is a language designed specifically to be easy to interpret by both humans and computers altogether. The language defines a set of rules used to encode a document in a specific format. In this article, methods have been described to read and write XM 8 min read Writing CSV files in Python CSV (Comma Separated Values) is a simple file format used to store tabular data, such as spreadsheets or databases. Each line of the file represents a data record, with fields separated by commas. This format is popular due to its simplicity and wide support.Ways to Write CSV Files in PythonBelow ar 11 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 Unzipping files in Python In this article we will see how to unzip the files in python we can achieve this functionality by using zipfile module in Python. What is a zip file ZIP file is a file format that is used for compressing multiple files together into a single file. It is used in an archive file format that supports l 3 min read Like