繁体   English   中英

Python-为什么fnmatch匹配子目录中的文件?

[英]Python - Why does fnmatch match files in subdirs?

为什么返回True

fnmatch('/home/user/a/b', '/home/user/*')

ls -d /home/user/*根本不提供/home/user/a/b

fnmatch仅阻止名称(字符串)-无需验证真实文件的存在。

要检查文件是否存在,可以使用os.path.exists(path)调用。 像这样:

from fnmatch import fnmatch
from os.path import exists

pattern = '/home/user/*'
name = '/home/user/a/b'

if exists(name):
    if fnmatch(name, pattern):
        print('"{}" exists and matches'.format(name))

暂无
暂无

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

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