- 1 year ago
Search .mp3 on computer using Python 3
Comment
1 / 1
To search for .mp3
files on your computer using Python 3, you can utilize the os
module to traverse the file system and filter the files based on their extension. Here's an example:
pythonimport os
# Define the root directory where you want to start the search
root_dir = 'C:\\'
# Function to recursively search for .mp3 files
def search_mp3_files(directory):
mp3_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.mp3'):
mp3_files.append(os.path.join(root, file))
return mp3_files
# Call the search_mp3_files function and print the results
mp3_files = search_mp3_files(root_dir)
for file in mp3_files:
print(file)
In the above example, you can modify the root_dir
variable to specify the directory from which you want to start the search. The search_mp3_files
function recursively walks through the directory tree, checks if each file has a .mp3
extension, and appends the absolute path of the file to the mp3_files
list.
After the search is complete, the code iterates over the mp3_files
list and prints the absolute path of each .mp3
file found.
Make sure to replace 'C:\'
with the appropriate root directory for your system.