[英]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.