Extracting extension from filename in Python

后端 未结 24 2404
感情败类
感情败类 2020-11-22 13:23

Is there a function to extract the extension from a filename?

24条回答
  •  無奈伤痛
    2020-11-22 14:19

    Although it is an old topic, but i wonder why there is none mentioning a very simple api of python called rpartition in this case:

    to get extension of a given file absolute path, you can simply type:

    filepath.rpartition('.')[-1]
    

    example:

    path = '/home/jersey/remote/data/test.csv'
    print path.rpartition('.')[-1]
    

    will give you: 'csv'

提交回复
热议问题