Getting file extension using pattern matching in python

后端 未结 6 1315
逝去的感伤
逝去的感伤 2020-12-11 19:58

I am trying to find the extension of a file, given its name as a string. I know I can use the function os.path.splitext but it does not work as expected in case

6条回答
  •  既然无缘
    2020-12-11 20:21

    Continuing from phihags answer to generic remove all double or triple extensions such as CropQDS275.jpg.aux.xml use while '.' in:

    tempfilename, file_extension = os.path.splitext(filename)
    while '.' in tempfilename:
         tempfilename, tempfile_extension = os.path.splitext(tempfilename)
         file_extension = tempfile_extension + file_extension
    

提交回复
热议问题