环境

Windows 10 x64
Python 3.7.6 x64

填充/补充零

zfill

a = 1
b = str(a).zfill(5)
print(a, b, type(b))

判断图片是否损坏

imghdr

import imghdr
if imghdr.what('filepath'):
    print('图片正常')
else:
    print('图片损坏')

补充

返回值 = imghdr.what(参数1, 参数2)
参数1: 图片本机路径/图片对象
参数2: 图片流,使用时,参数1无效,建议使用时,参数1手动 None
返回值: 图片类型,不能获取或图片损坏时返回值空,支持判断类型: rgb,gif,pbm,pgm,ppm,tiff,rast,xbm,jpeg,bmp,png,webp,exr

跳过本次循坏

continue

for i in range(10):
    if i == 4:
        print('{0}不吉利,跳过执行'.format(i))
        continue
    print('正常执行...{0}'.format(i))

结束循坏

break

for i in range(10):
    if i == 4:
        print('{0}不吉利,结束执行'.format(i))
        break
    print('正常执行...{0}'.format(i))

多进程

multiprocessing

Pool

from multiprocessing import Pool


def list_fz(listTemp, n):
    for i in range(0, len(listTemp), n):
        yield listTemp[i:i + n]


def worker(cs):
    # cs 为传入参数 i
    print(cs, type(cs))


def main():
    a =[1,2,3,4,5,6,7,8,9,0]
    po = Pool(2)
    count = 1
    for i in list_fz(a, 2):
        po.apply_async(worker, (i,)) # 必须使用(i,)传参,
        count = count + 1
    print('----------多线程 Start----------')
    po.close()
    po.join()
    print('----------多线程 End----------')


if __name__ == '__main__':
    main()
--------结果--------
----------多线程 Start----------
[1, 2] <class 'list'>
[3, 4] <class 'list'>
[5, 6] <class 'list'>
[7, 8] <class 'list'>
[9, 0] <class 'list'>
----------多线程 End----------
最后修改:2020 年 08 月 24 日 11 : 08 PM
如果觉得文章帮助了您,您可以随意赞赏。