python if后面不加else_Python if/else不工作。。。不满足if条件,但else不执行

嗯。我想出来了。结果是if在不匹配的时候并没有完全运行,这是因为_s3_bucket.list(prefix=basename)=[]。我恢复了对Python的信仰。:)谢谢大家!在

为什么这段代码从来没有打印“不匹配”?

当regex匹配时,它成功地打印“matches”,但如果不匹配,它就不执行else。print type(match)返回。在object_regex = re.compile('%s\.(\d+)\.%s' % \

(re.escape('.'.join(basename.split('.')[:-2])),

re.escape(basename.split('.')[-1])))

for obj in _s3_bucket.list(prefix=basename):

match = object_regex.findall(obj.name)

print match //prints nothing when nothing is found. not even []

if match:

print "matches"

else:

print "doesnt match"

我也试过了

^{pr2}$

它也从不执行if。在

打印输出(调用函数时上载到S3打印):UPLOAD TO S3 /fonts/HelveticaNeueLTPro-Lt.1351206175.otf

[u'1351206175']

matches

UPLOAD TO S3 /fonts/HelveticaNeueLTPro-Bd.1351206175.otf

[u'1351206175']

matches

UPLOAD TO S3 /css/common.1357015625.css

UPLOAD TO S3 /css/landing-style.1356896077.css

[u'1356896077']

matches

当我print type(match)而不是print match时,我每次都得到{},这让我相信匹配确实是一个空列表。但空名单应该能做到这一点。。。在>>> match=[]

>>> if match:

... print "ASDF"

...

>>> if not match:

... print "asdf"

...

asdf

你可能感兴趣的:(python,if后面不加else)