先贴几行代码:
def _get_paths_from_images(path):
assert os.path.isdir(path), '[Error] [%s] is not a valid directory' % path
images = []
for dirpath, _, fnames in sorted(os.walk(path)):
for fname in sorted(fnames):
if is_image_file(fname):
img_path = os.path.join(dirpath, fname)
images.append(img_path)
assert images, '[%s] has no valid image file' % path
return images
def _get_paths_from_binary(path):
assert os.path.isdir(path), '[Error] [%s] is not a valid directory' % path
files = []
for dirpath, _, fnames in sorted(os.walk(path)):
for fname in sorted(fnames):
if is_binary_file(fname):
binary_path = os.path.join(dirpath, fname)
files.append(binary_path)
assert files, '[%s] has no valid binary file' % path
return files
使用assert断言是学习python一个非常好的习惯,python assert 断言句语格式及用法很简单。在没完善一个程序之前,我们不知道程序在哪里会出错,与其让它在运行最崩溃,不如在出现错误条件时就崩溃,这时候就需要assert断言的帮助。
python assert断言是声明其布尔值必须为真的判定,如果发生异常就说明表达示为假。可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会触发异常。
eg:
assert 1==1
assert 2+2==2*2
assert len(['my boy',12])<10
assert range(4)==[0,1,2,3]
assert expression [, arguments]
assert 表达式 [, 参数]
例如:assert len(lists) >=5,'列表元素个数小于5'
assert 2==1,'2不等于1'