簡體   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