我试图在我的电脑中搜索音乐文件。我尝试了下面给出的代码,但它在文件名称完全相同时给出了结果。在python中的电脑中搜索音乐文件
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
现在,我想也搜索歌曲的错误拼写错误的谷歌也给出结果,如果拼写wrong.Like如果我们输入米哈尔·杰克逊,而不是迈克尔·杰克逊它给人的result.So为此,我尝试了下面的代码。
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)
,但我没有得到任何人results.Can告诉我什么是错误或为什么我没有收到。
快乐chinesh,如果这个答案解决您的问题,请考虑upvoting /接受它。 –
我不能这样做,因为我没有足够的声誉这样做。请提高我的问题,以便我可以帮助你:) – Berry
我已经...不用担心,我以为你不知道。 –