2016-09-04 28 views
-2

因此,假設我有一個包含大量文件名的目錄。 例如:我有一個文件名的一部分的列表,我想要通過與該部分相匹配的目錄中的文件並返回文件名

標積或點積(印地文)-fodZTqRhC24.m4a
AP物理C - 點產品,Wvhn_lVPiw0.m4a
介紹於點積-X5DifJW0zek.m4a

現在,讓我們說我有一個列表,只有鑰匙,這是在文件名末尾的:

['fodZTqRhC24', 'Wvhn_lVPiw0, 'X5DifJW0zek']

我怎樣可以通過我的列表迭代進入該目錄,搜索文件包含該密鑰的名稱,然後返回文件名?

任何幫助,非常感謝!

+0

你能展示你的最新嘗試並解釋什麼目前不適合你嗎?顯示確切的錯誤信息,如果有的話。 – idjaw

+0

它看起來像你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。展示這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出以及實際獲得的輸出(輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。檢查[FAQ](http://stackoverflow.com/tour)和[如何提問](http://stackoverflow.com/questions/how-to-ask)。 – TigerhawkT3

+0

好吧,你們是對的,我應該先嚐試一下。只是沒有線索如何嘗試它,想着正則表達式,但我需要通過它的字符串列表而不是字符串。無論如何,讓我花一些時間試圖找出答案,如果我卡住了,可以回頭看看。 – WhitneyChia

回答

0

下面是一個例子:

LS:文件在給定的目錄列表

名稱:字符串列表搜索

import os 
ls=os.listdir("/any/folder") 
n=['Py', 'sql'] 
for file in ls: 
    for name in names: 
    if name in file: 
     print(file) 

結果:

.PyCharm50 
.mysql_history 
zabbix2.sql 
.mysql 
PycharmProjects 
zabbix.sql 
+1

它應該是'for name in n:',因爲列表的名字是'n'。 – Harrison

0

假設您知道您將查找哪個目錄,您可以嘗試如下所示:

import os 

to_find = ['word 1', 'word 2']   # list containing words that you are searching for 
all_files = os.listdir('/path/to/file') # creates list with files from given directory 

for file in all_files: # loops through all files in directory 
    for word in to_find: # loops through target words 
     if word in file: 
      print file # prints file name if the target word is found 

我在目錄中含有這些文件進行測試這樣的:

Helper_File.py 
forms.py 
runserver.py 
static 
app.py 
templates 

...我設置to_find['runserver', 'static'] ...

,當我跑這個代碼,它返回:

runserver.py 
static 

爲了將來的參考,您至少應該進行某種嘗試在解決問題之前在Stackoverflow上發佈問題。如果你不能提供一個嘗試的證據,那麼人們不會這樣幫助你。

0

以下是一種方法,可根據文字的位置選擇天氣進行匹配。

import os 
def scan(dir, match_key, bias=2): 
    ''' 
     :0 startswith 
     :1 contains 
     :2 endswith 
     ''' 

    matches = [] 
    if not isinstance(match_key, (tuple, list)): 
     match_key = [match_key] 

    if os.path.exists(dir): 
     for file in os.listdir(dir): 
      for match in match_key: 
       if file.startswith(match) and bias == 0 or file.endswith(match) and bias == 2 or match in file and bias == 1: 
        matches.append(file) 
        continue 
    return matches 

print scan(os.curdir, '.py' 
+0

可能會更好地用'matcher'函數替換'bias',因爲幻數是不好的。然後,你可以執行'scan(...,matcher = str.startswith)'並在你的代碼中調用'if matcher(file,match):...' – Blender

1

我想過了,我想我已經比我用正則表達式更難了。抱歉,不要先嚐試它。我這樣做了:

audio = ['Scalar Product or Dot Product (Hindi)-fodZTqRhC24.m4a', 
'An Introduction to the Dot Product-X5DifJW0zek.m4a', 
'AP Physics C - Dot Product-Wvhn_lVPiw0.m4a'] 

keys = ['fodZTqRhC24', 'Wvhn_lVPiw0', 'X5DifJW0zek'] 

file_names = [] 
for Id in keys: 
    for name in audio: 
     if Id in name: 
      file_names.append(name) 

combined = zip(keys,file_names) 
combined    
+0

看看我發佈的答案。你的解決方案是有效的,但是它需要你知道目錄中所有文件的名字(因爲你在'audio'中硬編碼了目標單詞)。看看我如何使用'os.listdir()'來獲取文件。 – Harrison

+0

非常感謝Harrison,我使用'''file_list =!ls'''將目錄轉換爲列表,但我認爲你的方式更好。再次感謝! – WhitneyChia

相關問題