繁体   English   中英

fnmatch.fnmatch()未检测到正确的文件(或根本未检测到文件)

[英]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 ,但是返回file ,它恰好是Python 2中的内置函数。不确定为什么会显示'x ,但这就是为什么您看不到期望的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM