简体   繁体   English

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

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

Why does this return True : 为什么返回True

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

while ls -d /home/user/* doesn't give /home/user/a/b at all. ls -d /home/user/*根本不提供/home/user/a/b

fnmatch сhecks only names (strings) - without verification of the existence of real files. fnmatch仅阻止名称(字符串)-无需验证真实文件的存在。

To check file existence you may use os.path.exists(path) call. 要检查文件是否存在,可以使用os.path.exists(path)调用。 Like this: 像这样:

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