Search for a file using a wildcard

后端 未结 5 1529
闹比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:01

    I am adding this to the previous because I found this very useful when you want your scripts to work on multiple shell and with multiple parameters using *.

    If you want something that works on every shells, you can do the following (still using glob):

    >>> import glob
    >>> from functools import reduce # if using python 3+
    >>> reduce(lambda r, x: r + glob.glob(x), sys.argv[1:], [])
    

    Note that it can produce duplicate (if you have a test file and you give t* and te*), but you can simply remove them using a set:

    >>> set(reduce(lambda r, x: r + glob.glob(x), sys.argv[1:], []))
    

提交回复
热议问题