Search for a file using a wildcard

后端 未结 5 1532
闹比i
闹比i 2020-12-02 22:19

I want get a list of filenames with a search pattern with a wildcard. Like:

getFilenames.py c:\\PathToFolder\\*
getFilenames.py c:\\PathToFolder\\FileType*.t         


        
5条回答
  •  广开言路
    2020-12-02 23:04

    You can do it like this:

    >>> import glob
    >>> glob.glob('./[0-9].*')
    ['./1.gif', './2.txt']
    >>> glob.glob('*.gif')
    ['1.gif', 'card.gif']
    >>> glob.glob('?.gif')
    ['1.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']
    

    This comes straight from here: http://docs.python.org/library/glob.html

提交回复
热议问题