Python 如何从os.walk筛选文件(具有已知类型)?

Python 如何从os.walk筛选文件(具有已知类型)?,python,Python,我有os.walk中的列表。但是我想排除一些目录和文件。我知道如何使用目录: for root, dirs, files in os.walk('C:/My_files/test'): if "Update" in dirs: dirs.remove("Update") 但是我如何处理我知道的文件类型呢。因为这不起作用: if "*.dat" in files: files.remove("*.dat") 如果你经常这样做,一种简洁的写作方式: def exc

我有
os.walk
中的列表。但是我想排除一些目录和文件。我知道如何使用目录:

for root, dirs, files in os.walk('C:/My_files/test'):
    if "Update" in dirs:
        dirs.remove("Update")
但是我如何处理我知道的文件类型呢。因为这不起作用:

if "*.dat" in files:
    files.remove("*.dat")

如果你经常这样做,一种简洁的写作方式:

def exclude_ext(ext):
    def compare(fn): return os.path.splitext(fn)[1] != ext
    return compare

files = filter(exclude_ext(".dat"), files)

当然,排除在适当的实用程序包中。

应该正是您需要的:

files = [ fi for fi in files if not fi.endswith(".dat") ]
if thisFile.endswith(".txt"):
试试这个:

import os

skippingWalk = lambda targetDirectory, excludedExtentions: (
    (root, dirs, [F for F in files if os.path.splitext(F)[1] not in excludedExtentions]) 
    for (root, dirs, files) in os.walk(targetDirectory)
)

for line in skippingWalk("C:/My_files/test", [".dat"]):
    print line
这是一个生成lambda函数的生成器表达式。您向它传递一个路径和一些扩展名,它使用该路径调用os.walk,使用列表理解过滤掉不需要的扩展名列表中带有扩展名的文件,并返回结果


(编辑:删除了
.upper()
语句,因为不同大小写的扩展名之间可能存在实际差异-如果希望不区分大小写,请在
os.path.splitext(F)[1]
之后添加
.upper()
,并以大写字母传递扩展名。)

还有一种方式,因为我刚刚写了这个,然后偶然发现了这个问题:


files=filter(lambda文件:不是file.endswith('.txt'),文件)
排除多个扩展名

files = [ file for file in files if not file.endswith( ('.dat','.tar') ) ]

另一个解决方案是使用模块中的函数:


这样就避免了使用大写/小写扩展的所有麻烦。这意味着您不需要在必须匹配*.JPEG、*.JPEG、*.JPEG、*.JPEG、*.JPEG、*.JPEG时转换为lower/upper

使用os.WACK()筛选已知类型文件的最简单方法是告诉路径,并使用if语句获取扩展名筛选的所有文件

for base, dirs, files in os.walk(path):

    if files.endswith('.type'):

       #Here you will go through all the files with the particular extension '.type'
       .....
       .....

以上所有答案都有效。只是想为任何其他可能来自异构源的文件添加,例如从Internet下载存档中的图像。在这种情况下,由于类Unix系统区分大小写,您可能最终会使用扩展名“.PNG”和“.PNG”。这些字符串将被
endswith
方法视为不同的字符串,即
“.PNG”。endswith('PNG')
将返回
False
。为了避免此问题,请使用
lower()
函数。

不要说下一行不清楚要做什么。小心,os.walk返回一个生成器,生成根目录、目录和文件,
文件
是一个文件列表。我认为列表理解和过滤的答案让它变得清晰。
def MatchesExtensions(name,extensions=["*.dat", "*.txt", "*.whatever"]):
  for pattern in extensions:
    if fnmatch.fnmatch(pattern):
      return True
  return False
for base, dirs, files in os.walk(path):

    if files.endswith('.type'):

       #Here you will go through all the files with the particular extension '.type'
       .....
       .....