python2 递归函数

import os
import os.path as osp
import sys
import numpy as np
import datetime


def mkdirs_py2(path):
    # 递归创建文件夹路径
    if osp.exists(path): return

    paths = path.split('/')
    for i in range(1, len(paths)):
        _path = '/'.join(paths[:i + 1])
        if not osp.exists(_path):
            os.makedirs(_path)


def glob_files_py2(read_path, format=0):
    """
    find all files by format
    :param read_path:
    :param format: 0=pic 1=video
    :return:
    """
    import re
    filelist = []
    pattern = None
    if format == 0:
        pattern = re.compile(r'.(png|jpeg|jpg|bmp|tif|tiff)$')
    elif format == 1:
        pattern = re.compile(r'.(avi|mp4|h264|264|dav|mkv|wmv)$')
    assert pattern is not None
    for r, dirs, files in os.walk(read_path):
        for file in files:
            if re.search(pattern, file) is not None:
                f = '/'.join([r, file])
                if osp.isfile(f): filelist.append(f)
    return filelist


def iglob_files_py2(read_path, format=0):
    """
    find all files by format
    :param read_path:
    :param format: 0=pic 1=video
    :return:
    """
    import re
    pattern = None
    if format == 0:
        pattern = re.compile(r'.(png|jpeg|jpg|bmp|tif|tiff)$')
    elif format == 1:
        pattern = re.compile(r'.(avi|mp4|h264|264|dav|mkv|wmv)$')
    assert pattern is not None
    for r, dirs, files in os.walk(read_path):
        for file in files:
            if re.search(pattern, file) is not None:
                f = '/'.join([r, file])
                if osp.isfile(f):
                    yield f


def get_now_time():
    now = datetime.datetime.now()
    return datetime.datetime.strftime(now, '%Y-%m-%d %H:%M:%S')

 

posted @ 2022-11-09 23:05  dangxusheng  阅读(19)  评论(0编辑  收藏  举报