出色地。我想到了。事实证明,if 在不应该匹配的时候并没有准确地运行,那是因为_s3_bucket.list(prefix=basename)
= []。我对 python 的信心已经恢复。:) 谢谢你们!
为什么这段代码永远不会打印“不匹配”?当正则表达式匹配时,它会成功打印“匹配”,但如果不匹配,则不会执行 else。print type(match)
返回<type 'list'>
。
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"
我也试过
if not match:
print "doesnt match"
else:
print "matches"
它也从不执行 if 。
打印输出(调用函数时打印 UPLOAD TO 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 <-- what is going on here?
UPLOAD TO S3 /css/landing-style.1356896077.css
[u'1356896077']
matches
当我print type(match)
不是每次都print match
得到<type 'list'>
时,这让我相信匹配确实是一个空列表。但是空列表应该这样做......
>>> match=[]
>>> if match:
... print "ASDF"
...
>>> if not match:
... print "asdf"
...
asdf