0
我想编写一个python代码,当给出一个字符串(要搜索的mp3文件的名称)时,它会搜索与文件名匹配的给定字符串的目录。找到后,它会打开并播放它。如何使用Python 3从文件夹/目录搜索和播放mp3文件?
我有一个代码来搜索给定的目录给定的字符串。是否可以修改此代码以实现上述任务?谢谢。 `
#Import os module
import os
# Ask the user to enter string to search
search_path = input("Enter directory path to search : ")
file_type = input("File Type : ")
search_str = input("Enter the search string : ")
# Append a directory separator if not already present
if not (search_path.endswith("/") or search_path.endswith("\\")):
search_path = search_path + "/"
# If path does not exist, set search path to current directory
if not os.path.exists(search_path):
search_path ="."
# Repeat for each file in the directory
for fname in os.listdir(path=search_path):
# Apply file type filter
if fname.endswith(file_type):
# Open file for reading
fo = open(search_path + fname)
# Read the first line from the file
line = fo.readline()
# Initialize counter for line number
line_no = 1
# Loop until EOF
while line != '' :
# Search for string in line
index = line.find(search_str)
if (index != -1) :
print(fname, "[", line_no, ",", index, "] ", line, sep="")
# Read next line
line = fo.readline()
# Increment line counter
line_no += 1
# Close the files
fo.close()
一旦找到文件,'os.startfile('filename.mp3')'会播放它。 –
[在python上播放mp3歌曲]的可能重复(https://stackoverflow.com/questions/20021457/playing-mp3-song-on-python) –
因此使用os.startfile我将能够播放MP3的权利?或者我会需要pygame,pyglet或任何其他媒体模块的Python? –