Extracting extension from filename in Python

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

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

24条回答
  •  花落未央
    2020-11-22 13:59

    With splitext there are problems with files with double extension (e.g. file.tar.gz, file.tar.bz2, etc..)

    >>> fileName, fileExtension = os.path.splitext('/path/to/somefile.tar.gz')
    >>> fileExtension 
    '.gz'
    

    but should be: .tar.gz

    The possible solutions are here

提交回复
热议问题