pythonstring

What's the elegant way to get filename that contains more than one dot in python


I need to process linux file with multiple parts in the name like:

abc.def.gh.bz2

bz2 is the real extension, I need to get abc.def.gh

Can anyone tell me an elegant way to do this in python?

Thank you very much.


Solution

  • Use os.path.splitext:

    import os.path
    os.path.splitext("abc.def.gh.bz2") # -> ("abc.def.gh", ".bz2")