Python无论如何

Agi*_*eas 1 python if-statement

我有一个if语句,如下所示

if (not(fullpath.lower().endswith(".pdf")) or 
    not (fullpath.lower().endswith(tuple(settings.imageExtensions)))) :
Run Code Online (Sandbox Code Playgroud)

问题是,如果文件以.pdf或其他扩展名之一结束,那么这些都不起作用并且它会进入if函数.做这个的最好方式是什么?仅供参考我不能把.pdf放在元组中

Cor*_*mer 6

您想and根据DeMorgan的法律进行切换

if not(fullpath.lower().endswith(".pdf")) and not (fullpath.lower().endswith(tuple(settings.imageExtensions))) :
Run Code Online (Sandbox Code Playgroud)

换句话说,以下是等同的

not A and not B == not (A or B)
Run Code Online (Sandbox Code Playgroud)