2016-05-19 14 views
0

我试图在我的电脑中搜索音乐文件。我尝试了下面给出的代码,但它在文件名称完全相同时给出了结果。在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告诉我什么是错误或为什么我没有收到。

回答

0

尝试glob包,喜欢的东西:

for file in glob.glob('G:\\*\\*.mp3"): 
    print(file) 

这可能不是可能是你完整的答案,但我想你可以从这里到达那里。

0

SequenceMatcher你正在比较一个列表中的几个文件名与一个列表中只包含你正在寻找。因此,不匹配的结果。 尝试以下操作:

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]): 
    for filename in filenames: 
     d = difflib.SequenceMatcher(None,filename, to_search).ratio() 
     if d>=0.6: 
      matches=(os.path.join(root, filename)) 

webbrowser.open(matches) 
+0

快乐chinesh,如果这个答案解决您的问题,请考虑upvoting /接受它。 –

+0

我不能这样做,因为我没有足够的声誉这样做。请提高我的问题,以便我可以帮助你:) – Berry

+0

我已经...不用担心,我以为你不知道。 –