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