【发布时间】:2025-12-16 08:00:02
【问题描述】:
我正在尝试在我的计算机中搜索音乐文件。我尝试了下面给出的代码,但它会在文件名完全相同时给出结果。
import fnmatch
import os
import webbrowser
song = raw_input("enter the song")
to_search = [song + '.mp3']
path = ['G:\\','F:\\','E:\\']
for k in path:
for root, dirnames, filenames in os.walk(k):
for extensions in to_search:
for filename in fnmatch.filter(filenames, extensions):
matches=(os.path.join(root, filename))
print matches
if not matches:
print ("no such song is found")
else:
webbrowser.open(matches) #play the song if found
现在我也在尝试搜索这首歌是否有错误的拼写错误,因为如果拼写错误,谷歌也会给出结果。就像我们输入 Michal jackson 而不是 Michael jackson 它会给出结果。所以为此我尝试了以下代码。
import os
import webbrowser
import difflib
song = raw_input("enter the song")
to_search = [song + '.mp3']
path = ['E:\\']
for root, dirnames, filenames in os.walk(path[0]):
d = difflib.SequenceMatcher(None,filenames, to_search).ratio()
if d>=0.6:
print d
matches=(os.path.join(root,filenames))
webbrowser.open(matches)
但我没有得到结果。谁能告诉我错误是什么或为什么我没有得到。
【问题讨论】:
标签: python python-2.7 search