fnmatch.fnmatch() 未检测到正确的文件(或根本检测不到 none)
fnmatch.fnmatch() not detecting correct file (or detecting none at all)
给定当前工作目录中名为 SI 01130 TN 72 - 2016-02-19 的文件,以及以下内容:
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
你迭代和测试_file
,但是return file
,这恰好是Python中的内置函数 2. 不确定为什么它打印'x
,但这就是为什么您没有看到预期的结果。
给定当前工作目录中名为 SI 01130 TN 72 - 2016-02-19 的文件,以及以下内容:
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
你迭代和测试_file
,但是return file
,这恰好是Python中的内置函数 2. 不确定为什么它打印'x
,但这就是为什么您没有看到预期的结果。