Extracting extension from filename in Python

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

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

24条回答
  •  名媛妹妹
    2020-11-22 14:01

    Any of the solutions above work, but on linux I have found that there is a newline at the end of the extension string which will prevent matches from succeeding. Add the strip() method to the end. For example:

    import os.path
    extension = os.path.splitext(filename)[1][1:].strip() 
    

提交回复
热议问题