pyutil.py 1.02 KB
# -*- coding: UTF-8 -*-

def printStackTrace(filename):
    from sys import exc_info
    from os.path import basename
    print("\n[Exception begin]\n  File: %s\n  Name: %s\n  Line: %s\n  Type: %s\n  Message: %s\n[Exception end]\n" % (
            basename(filename), # basename(exc_info()[2].tb_frame.f_code.co_filename),
            exc_info()[2].tb_frame.f_code.co_name,
            exc_info()[2].tb_lineno,
            exc_info()[0].__name__,
            exc_info()[1],
        )
    )

def file_rename(filename, fromfile):
    from shutil import move
    newFilename = ""
    isValidChar = True
    for char in reversed(filename):
        if (isValidChar == True):
            newFilename += char
        if (char == '_'):
            isValidChar = True
        elif (char == '.'):
            isValidChar = False
    if (len(filename) != len(newFilename)):
        try:
            move(filename, newFilename[::-1])
            return 1
        except Exception as e:
            printStackTrace(fromfile)
            return 0
    return 0