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