kitir/_libs/file_utils.py
def bulk_rename(src_dir, before, after, dst_dir=None, raise_on_error=True):
    """
    Perform bulk-rename operation on files in a directory. optionally move them to another directory.
    :param src_dir:
    :param before:
    :param after:
    :param dst_dir:
    :param raise_on_error:
    :return:
    """
    assert before != after
    dst_dir = dst_dir or src_dir
    log.debug('bulk-renaming: src={} dst={} before={} after={}'.format(src_dir, dst_dir, before, after))
    fns = [fn for fn in os.listdir(src_dir) if os.path.isfile(fn) and before in fn]
    if not fns:
        log.warning('bulk-rename found no files to work with')
        return False
    success = 0
    for fn in fns:
        fp = os.path.join(src_dir, fn)
        nfp = os.path.join(dst_dir, fn.replace(before, after))
        log.trace('bulk-renaming: src={} dst={}'.format(fp, nfp))
        try:
            shutil.move(fp, nfp)
        except Exception as exc:
            if not raise_on_error:
                log.warning('Exception bulk-renaming files, ignoring: exc={}'.format(exc))
            raise
        else:
            success += 1
    if success == len(fns):
        return True
    log.warning('bulk-rename had failures: fails={}'.format(len(fns) - success))
    return False

    Perform bulk-rename operation on files in a directory. optionally move them to another directory.
    :param src_dir:
    :param before:
    :param after:
    :param dst_dir:
    :param raise_on_error:
    :return:
    
Perform bulk-rename operation on files in a directory. optionally move them to another directory.
Perform bulk - rename operation on files in a directory . optionally move them to another directory .
def bulk_rename(src_dir, before, after, dst_dir=None, raise_on_error=True):
    assert before != after
    dst_dir = dst_dir or src_dir
    log.debug('bulk-renaming: src={} dst={} before={} after={}'.format(src_dir, dst_dir, before, after))
    fns = [fn for fn in os.listdir(src_dir) if os.path.isfile(fn) and before in fn]
    if not fns:
        log.warning('bulk-rename found no files to work with')
        return False
    success = 0
    for fn in fns:
        fp = os.path.join(src_dir, fn)
        nfp = os.path.join(dst_dir, fn.replace(before, after))
        log.trace('bulk-renaming: src={} dst={}'.format(fp, nfp))
        try:
            shutil.move(fp, nfp)
        except Exception as exc:
            if not raise_on_error:
                log.warning('Exception bulk-renaming files, ignoring: exc={}'.format(exc))
            raise
        else:
            success += 1
    if success == len(fns):
        return True
    log.warning('bulk-rename had failures: fails={}'.format(len(fns) - success))
    return False
def bulk_rename ( src_dir , before , after , dst_dir = None , raise_on_error = True ) : assert before != after dst_dir = dst_dir or src_dir log . debug ( 'bulk-renaming: src={} dst={} before={} after={}' . format ( src_dir , dst_dir , before , after ) ) fns = [ fn for fn in os . listdir ( src_dir ) if os . path . isfile ( fn ) and before in fn ] if not fns : log . warning ( 'bulk-rename found no files to work with' ) return False success = 0 for fn in fns : fp = os . path . join ( src_dir , fn ) nfp = os . path . join ( dst_dir , fn . replace ( before , after ) ) log . trace ( 'bulk-renaming: src={} dst={}' . format ( fp , nfp ) ) try : shutil . move ( fp , nfp ) except Exception as exc : if not raise_on_error : log . warning ( 'Exception bulk-renaming files, ignoring: exc={}' . format ( exc ) ) raise else : success += 1 if success == len ( fns ) : return True log . warning ( 'bulk-rename had failures: fails={}' . format ( len ( fns ) - success ) ) return False
Perform bulk-rename operation on files in a directory.
Perform bulk - rename operation on files in a directory .
"""
    Perform bulk-rename operation on files in a directory. optionally move them to another directory.
    :param src_dir:
    :param before:
    :param after:
    :param dst_dir:
    :param raise_on_error:
    :return:
    """
None
src_dir
None
before
None
after
None
dst_dir
None
raise_on_error
None