Search code examples
pythonregexfile-extensionstrip

Regex to select a file extension


I have an apk file say MyApp.apk. I was trying to strip the .apk extension using the strip function in python. But the problem is, If my applications name is WhatsApp.apk then the function strips the letters pp also and outputs WhatsA. What Regex should I use to strip exactly the .apk away?


Solution

  • Why use regex? If you only want the filename then this code will do

    filename = 'MyApp.apk'
    filename = filename.rsplit('.', 1)[0]
    
    print filename
    

    output:

    MyApp