Extracting specific files within directory - Windows

前端 未结 2 1544
盖世英雄少女心
盖世英雄少女心 2021-01-25 22:05

I am running a loop which needs to access circa 200 files in the directory.

In the folder - the format of the files range as follows:

  1. Excel_YYYYMMDD.txt<
相关标签:
2条回答
  • 2021-01-25 22:21

    Here is a cheap way to do it (and by cheap I mean probably not the best/cleanest method):

    import glob
    l = glob.glob("Excel_[0-9]*.txt")
    

    This will get you:

    >>> print(l)
    ['Excel_19900717_orig.txt', 'Excel_19900717_V2.txt', 'Excel_19900717.txt']
    

    Now filter it yourself:

    nl = [x for x in l if "_orig" not in x and "_V2" not in x]
    

    This will give you:

    >>> print(nl)
    ['Excel_19900717.txt']
    

    The reason for manually filtering through our glob is because the glob library does not support regex.

    0 讨论(0)
  • 2021-01-25 22:39

    Use ^Excel_[0-9]{8}\.txt as the file matching regex.

    0 讨论(0)
提交回复
热议问题