python: rstrip one exact string, respecting order

前端 未结 6 2115
挽巷
挽巷 2020-12-16 09:16

Is it possible to use the python command rstrip so that it does only remove one exact string and does not take all letters separately?

I was confused w

6条回答
  •  醉话见心
    2020-12-16 10:09

    This will work regardless of extension type.

    # Find the rightmost period character
    filename = "my file 1234.txt"
    
    file_extension_position = filename.rindex(".")
    
    # Substring the filename from first char up until the final period position
    stripped_filename = filename[0:file_extension_position]
    print("Stripped Filename: {}".format(stripped_filename))
    

提交回复
热议问题