假設您知道您將查找哪個目錄,您可以嘗試如下所示:
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上發佈問題。如果你不能提供一個嘗試的證據,那麼人們不會這樣幫助你。
你能展示你的最新嘗試並解釋什麼目前不適合你嗎?顯示確切的錯誤信息,如果有的話。 – idjaw
它看起來像你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。展示這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出以及實際獲得的輸出(輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。檢查[FAQ](http://stackoverflow.com/tour)和[如何提問](http://stackoverflow.com/questions/how-to-ask)。 – TigerhawkT3
好吧,你們是對的,我應該先嚐試一下。只是沒有線索如何嘗試它,想着正則表達式,但我需要通過它的字符串列表而不是字符串。無論如何,讓我花一些時間試圖找出答案,如果我卡住了,可以回頭看看。 – WhitneyChia