簡體   English   中英

用python在計算機中搜索音樂文件

[英]music file search in computer in 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

現在我正嘗試在歌曲中搜索錯誤的拼寫錯誤,因為如果拼寫錯誤,谷歌也會給出結果。例如,如果我們鍵入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)

但是我沒有得到結果。誰能告訴我是什么錯誤或為什么我沒有得到結果。

嘗試使用glob軟件包,例如:

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

這可能不是您的全部答案,但我想您可以從這里到達那里。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM