小编Luc*_*ams的帖子

使用“**”模式的递归 glob.glob 出现意外结果

不存在的目录上的递归 glob 结果:

>>> import os, glob
>>> os.path.exists('dir')
False

>>> glob.glob('dir/*', recursive=True)
[]

>>> glob.glob('dir/**', recursive=True)
['dir/']

Run Code Online (Sandbox Code Playgroud)

现有文件的递归 glob 结果作为目录返回:

>>> os.path.exists('file')
True

>>> glob.glob('file/*', recursive=True)
[]

>>> glob.glob('file/**', recursive=True)
['file/']

Run Code Online (Sandbox Code Playgroud)

使用 bash shell 补全的类似命令将产生以下输出:

$ shopt -s globstar failglob

$ ls dir
ls: cannot access 'dir': No such file or directory
$ echo dir/*
-bash: no match: dir/*
$ echo dir/**
-bash: no match: dir/**

$ touch file
$ echo file/*
-bash: no match: file/*
$ echo file/** …
Run Code Online (Sandbox Code Playgroud)

python glob

6
推荐指数
0
解决办法
170
查看次数

标签 统计

glob ×1

python ×1