Getting file extension using pattern matching in python

后端 未结 6 1318
逝去的感伤
逝去的感伤 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

    root,ext = os.path.splitext('a.tar.gz')
    if ext in ['.gz', '.bz2']:
       ext = os.path.splitext(root)[1] + ext
    

    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

提交回复
热议问题