假设您知道您将查找哪个目录,您可以尝试如下所示:
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