使用通配符搜索文件 [英] Search for a file using a wildcard
本文介绍了使用通配符搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取带有通配符的搜索模式的文件名列表.喜欢:
I want get a list of filenames with a search pattern with a wildcard. Like:
getFilenames.py c:\PathToFolder\*
getFilenames.py c:\PathToFolder\FileType*.txt
getFilenames.py c:\PathToFolder\FileTypeA.txt
我该怎么做?
推荐答案
您可以这样做:
>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
注意:
如果目录包含以.
开头的文件,则默认情况下将不匹配它们.例如,考虑包含card.gif
和.card.gif
的目录:
Note:
If the directory contains files starting with .
they won’t be matched by default. For example, consider a directory containing card.gif
and .card.gif
:
>>> import glob
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']
这直接来自这里: http://docs.python.org/library/glob .html
这篇关于使用通配符搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文