python应用-获取文件后缀名

def get_suffix(filename,has_dot=False):
    """
    获取文件后缀名
    :param filename: 文件名
    :param has_dot: 后缀名是否带.
    :return: 文件后缀名
    """
    pos=filename.rfind('.')
    if 0<pos<len(filename)-1:
        index=pos if has_dot else pos+1
        return filename[index:]
    else:
        return ''
if __name__ == '__main__':
    print(get_suffix('hello.jpg'))
    print(get_suffix('abc.',True))
    print(get_suffix('a.'))
    print(get_suffix('hello.c',True))
    print(get_suffix('adc.abc'))

  结果: jpg .c abc

原文地址:https://www.cnblogs.com/68xi/p/8546426.html