我试图想出一个regex
只过滤掉一种或多种特定类型(扩展名)的文件,同时使用os.walk
. 我的文件夹结构(待搜索)如下所示。注意没有扩展名的文件。
Directory: D:\Projects\5 Codes Cleaned\2012
SG
|---SG.zip
|---SOIL-Average.jpg
|---SWAT-Average.jpg
|---Test
1500_LT_Capped_2012
1500_LT_Capped_2012
1500_LT_Capped_2012
1500_LT_Capped_2012
1500_LT_Capped_2012
1500_LT_Capped_2012
1500_LT_Capped_2012
1500_LT_Capped_2012
PRESSURE-Average.png
SGAS-Average.png
SOIL-Average.png
SWAT-Average.png
或列表格式:
[u'D:\\Projects\\5 Codes Cleaned\\2012\\1500_LT_Capped_2012 -P',
u'D:\\Projects\\5 Codes Cleaned\\2012\\1500_LT_Capped_2012 -P.npy',
u'D:\\Projects\\5 Codes Cleaned\\2012\\1500_LT_Capped_2012 -Sg',
u'D:\\Projects\\5 Codes Cleaned\\2012\\1500_LT_Capped_2012 -Sg.npy',
u'D:\\Projects\\5 Codes Cleaned\\2012\\1500_LT_Capped_2012 -So',
u'D:\\Projects\\5 Codes Cleaned\\2012\\1500_LT_Capped_2012 -So.npy',
u'D:\\Projects\\5 Codes Cleaned\\2012\\1500_LT_Capped_2012 -Sw',
u'D:\\Projects\\5 Codes Cleaned\\2012\\1500_LT_Capped_2012 -Sw.npy',
u'D:\\Projects\\5 Codes Cleaned\\2012\\PRESSURE-Average.png',
u'D:\\Projects\\5 Codes Cleaned\\2012\\SGAS-Average.png',
u'D:\\Projects\\5 Codes Cleaned\\2012\\SOIL-Average.png',
u'D:\\Projects\\5 Codes Cleaned\\2012\\SWAT-Average.png',
u'D:\\Projects\\5 Codes Cleaned\\2012\\SG\\SG.zip',
u'D:\\Projects\\5 Codes Cleaned\\2012\\SG\\SOIL-Average.jpg',
u'D:\\Projects\\5 Codes Cleaned\\2012\\SG\\SWAT-Average.jpg',
u'D:\\Projects\\5 Codes Cleaned\\2012\\SG\\Test']
我查看了这里的一些线程以获得一些想法,但我想知道是否有更简单的方法可以做到这一点。到目前为止,我已经尝试了以下模式来过滤掉os.walk
结果:
regex = "^.*(?<!\.png)(?<!\.npy)$"
# The only working one but tends to get messy
# as more file types are to be excluded!
&
regex = "^(.+?)(?:\.(?:png|jpg))*$"
# Does not filter out jpg or png...list all files
&
regex = '^.*\.(?!jpg$|png$)[^.]+$'
# Filters out png & jpg but Does not include No-Extensions !
&
regex = '^.*\.*(?!.jpg$|.png$)'
# Does not filter out png & jpg file