Python-从列表中忽略字符串不起作用

贾加德什

编写下面的代码以基于“忽略”列表选择目录:

mypath = 'c:\\Windows\\help'
ignorelist = ['mui', 'en-US']
for root, directories, filenames in walk(mypath):
    for directory in directories :
        if not any(ignorestring in directory for ignorestring in ignorelist):
            print(join(root,directory))

当ignorelist变量列表为空时,输出如下:

c:\Windows\help\Corporate
c:\Windows\help\Help
c:\Windows\help\mui
c:\Windows\help\OEM
c:\Windows\help\Windows
c:\Windows\help\Help\en-US
c:\Windows\help\mui\0409
c:\Windows\help\Windows\en-US

在忽略列表中带有['mui','en-US']的输出为:

c:\Windows\help\Corporate
c:\Windows\help\Help
c:\Windows\help\OEM
c:\Windows\help\Windows
c:\Windows\help\mui\0409

即使mui在忽略列表中,“ c:\ Windows \ help \ mui \ 0409”也不会被忽略。

请说明为什么会这样以及如何忽略mui文件夹的所有子目录

pp

您需要应用标准,path而不仅仅是directory

from os.path import join
from os import walk

mypath = 'c:\\Windows\\help'
ignorelist = ['mui', 'en-US']

for root, directories, filenames in walk(mypath):
    for directory in directories:
        path = join(root, directory)
        if not any(ignorestring in path for ignorestring in ignorelist):
            print(path)

c:\Windows\help\Corporate
c:\Windows\help\Help
c:\Windows\help\OEM
c:\Windows\help\Windows

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从Python .remove中的列表中删除字符串元素对我不起作用

减少python中的字符串不起作用

Python-在字符串中查找子字符串(代码不起作用)

Python - 创建单独的列表,但整数和字符串不起作用

使用replace方法在Python中替换字符串中的值不起作用

为什么Java中的这种反向字符串逻辑在python中不起作用?

用python babel翻译字符串不起作用

Python字符串模式匹配不起作用

非常基本的字符串格式不起作用(Python)

Python分割字符串不起作用

Python解析字符串替换不起作用

字符串比较不起作用!语言是python

不等于运算符(!=)在python字符串比较中不起作用

字符串到 int/float 转换在树莓派上的 python 中不起作用

循环中的Jupyter Notebook中的Python F字符串不起作用

IP地址字符串提取在Python3中不起作用

Python程序不起作用,输入==字符串不起作用

当字符串和子字符串的长度相等时,Python 中字符串的 find() 是否不起作用?

在Python 3.6中,为什么字符串文字(使用相同的引用类型)在插值字符串中不起作用?

UDF对字符串进行编码在psql和Perl中有效,但在Python中不起作用

python枚举按字符串值获取不起作用

带有Unicode字符串的Python正则表达式不起作用

Python将布尔值连接到字符串不起作用

带字符串的 Python 密钥生成器不起作用

从bash脚本重定向时,将变量与字符串python比较不起作用

Python删除空字符串和strip()不起作用

(python)用户定义的异常不起作用,使用类{不知道为什么当我输入字符串时异常不起作用}

Python列表副本在python 3.6中不起作用

在 Python 中从字符串创建列表