2016-02-20 95 views
0

給定一個稱爲SI 01130 TN 72文件 - 2016年2月19日在當前工作目錄,以及以下:fnmatch.fnmatch()未檢測正確的文件(或根本檢測無)

import os 
import fnmatch 

def matching_current_instruction_id_in_baseline(_id): 
    for _file in os.listdir('.'): 
     if fnmatch.fnmatch(_file, 'SI 01130 TN 72*'): 
      return file 
     else: 
      continue 
    return None 

爲什麼以下文件沒有找到(所有匹配的文件都是由於某種原因被稱爲'x'的文件)。

In[50]: matching_current_instruction_id_in_baseline('SI 01130 TN 72') 
Out[50]: 'x 
+1

不是一個答案,但glob會是一個更好的方法,特別是iglob,如果你只是想看看是否有任何匹配,你的其他繼續也是多餘的。 fnmatch也會匹配發布的字符串,以便進行其他操作。 –

+1

'return next(glob.iglob(「path_to /'SI 01130 TN 72 *'」),None)''將完成當前函數的所有功能。 –

+1

您正在使用名爲'_file'的變量,但是返回'file'。修復後,它適用於我。 – jsfan

回答

1

你迭代和測試_file,但返回file,這恰好是在Python 2。不知道爲什麼它打印'x一個內置的功能,但是這就是爲什麼你沒有看到你所期望的。