在python中,如果文件以一组特定的字母开头,如何从循环中排除这些文件?

在python中,如果文件以一组特定的字母开头,如何从循环中排除这些文件?,python,string,Python,String,我正在编写一个Python脚本,它遍历一个目录并收集某些文件,但是我想排除一些文件,它们的开头都是一样的 示例代码: for name in files: if name != "doc1.html" and name != "doc2.html" and name != "doc3.html": print name 假设目录中有100个HTML文件,都以'doc'开头。排除它们的最简单方法是什么 抱歉,我是Python新手,我知道这可能是最基本的 提前谢谢 for nam

我正在编写一个Python脚本,它遍历一个目录并收集某些文件,但是我想排除一些文件,它们的开头都是一样的

示例代码:

for name in files:
   if name != "doc1.html" and name != "doc2.html" and name != "doc3.html":
      print name
假设目录中有100个HTML文件,都以
'doc'
开头。排除它们的最简单方法是什么

抱歉,我是Python新手,我知道这可能是最基本的

提前谢谢

for name in files:
    if not name.startswith("doc"):
        print name
如果要排除更多前缀,甚至可以执行以下操作:

if not name.startswith(('prefix', 'another', 'yetanother')):
     print name

可以接受一组前缀。

如果所有前缀都以相同的开头(即以“doc”开头),则可以使用python字符串的startswith()方法

for name in files:
    if name[0:3] == "doc":
         continue

由于您没有说明是否存在以“doc”开头、以“.html”结尾的好文件,因此您必须声明一组坏文件名,并仅处理不在该组中的文件

bad_files = set(["doc1.html", "doc2.html", "doc3.html"])

for file in files:
  if file not in bad_files:
    print file

如果需要动态更改文件名列表,请使用
列表

如果发现函数式编程更符合您的风格,Python可以使用filter()函数简化列表过滤:

import os
os.chdir("/home")
for file in os.listdir("."):
   if os.path.isfile(file) and not file.startswith("doc"):
      print file
>>> files = ["doc1.html", "doc2.html", "doc3.html", "index.html", "image.jpeg"]
>>> filter_function = lambda name: not name.startswith("doc")
>>> filter(filter_function, files)
['index.html', 'image.jpeg']

还可以看看apply()、map()、reduce()和zip() 正如Troy所说(尽管我更喜欢将函数直接放入过滤器)

你也可以用一个


此问题的功能解决方案的另一种选择,其优点是使用标准库中最近添加的内容(使用与Troy J.Farrell在另一个答案中使用的文件名相同的示例):

operator.methodcaller
使用
methodname调用,[可选参数]
返回一个函数,当使用对象
obj
作为其参数调用时,该函数返回
obj.methodname(可选参数)的结果。
itertools.ifilterfalse
,与
filter
不同,它返回一个迭代器而不是一个列表,并且过滤决定被否定。

这是我的2美分:
一点列表理解。这对效率总是有好处的

file_list = [file for file in directory if not file.startswith(("name1", "name2", "name3"))]

在遍历文件夹中的所有文件时,跳过要排除的文件。 下面的代码将跳过所有以“doc”开头的html文件

import glob
import re
for file in glob.glob('*.html'):
    if re.match('doc.*\.html',file):
        continue
    else:
        #do your stuff here
        print(file)

我要找的是startswith的反面,如果有一个名为doesnotstartwith()的方法,我会被排序:)对不起,不知道我怎么会错过它,至少使用集合而不是列表。集合查找为O(1),列表查找为O(N)。@Nadia Alramli更改为示例,就像您建议的一样。您将其更改为元组,而不是集合。它应该是:bad_files=set([“doc1.html”、“doc2.html”、“doc3.html”])有时我就是想不起来——这些问题如何以及为什么会得到4票。除了被愚弄和其他一些问题之外,它绝对是微不足道的,并且显示出对语言工具的基本认识不足provides@Eli当前位置你是说我们应该忽略那些琐碎和基本的问题吗?丹尼尔:我想他不是说不要问简单的问题,只是说人们不应该投票支持他们。不幸的是,复杂的问题可能会吸引更狭隘的听众,获得更少的选票。哦,伊莱,这就是你的答案。@Ruth:我完全不反对你问这个问题。我很高兴你得到帮助,这就是为什么。我对扭曲现实的投票感到不安sometimes@telliott99,upvote的意思是(我直接引用鼠标悬停文本)“这个问题很有用,也很清楚”。这既不包括“值得注意的”也不包括“有趣的”。我敢肯定,许多人之所以投票,仅仅是因为问题简洁明了。但请注意,
apply()
filter(lambda filename: not filename.startswith("doc"),files)
[filename for filename in files if not filename.startswith("doc")]
cleaned_list = [filename for filename in files if not filename.startswith('doc')]
>>> import operator, itertools
>>> filter_fun= operator.methodcaller("startswith", "doc")
>>> files = ["doc1.html", "doc2.html", "doc3.html", "index.html", "image.jpeg"]
>>> list(itertools.ifilterfalse(filter_fun, files))
['index.html', 'image.jpeg']
file_list = [file for file in directory if not file.startswith(("name1", "name2", "name3"))]
import glob
import re
for file in glob.glob('*.html'):
    if re.match('doc.*\.html',file):
        continue
    else:
        #do your stuff here
        print(file)