2016-09-04 27 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

相关问题