2

我试图想出一个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
4

2 回答 2

2

为什么不只使用os.path.splitext和列表理解?

disallowed_types = ['png', 'jpg']

allowed = [x for x in allfiles if os.path.splitext(x)[1] not in disallowed_types]

但如果你必须使用正则表达式,这似乎工作,但倒置:

regex = '[^.]*?\.+(jpg$|png$)'

因此,如果它与此匹配,则它是 jpg 或 png 并且不应包含在内,否则它是安全的并且可以包含在列表中。

于 2013-10-24T18:11:53.290 回答
0

为什么不只是:

>>> extensions = "png jpg npy".split()
>>> regex = "^.*%s$"
>>> regex%"".join("(?<!\.%s)"%i for i in extensions)
'^.*(?<!\\.png)(?<!\\.jpg)(?<!\\.npy)$'

接着:

all = [...]
# If you want to get all files with extension `'jpg', 'png', '...'`:

[i for i in [compiled.match(x) for x in a] if not i]

# Will get you all files with no extension, with .png or with .jpg

# If you want all OTHER files (like .zip):

[i for i in [compiled.match(x) for x in a] if i]
于 2013-10-24T18:45:48.967 回答