How to replace (or strip) an extension from a filename in Python?

前端 未结 7 511
暗喜
暗喜 2020-12-01 05:02

Is there a built-in function in Python that would replace (or remove, whatever) the extension of a filename (if it has one) ?

Example:

print replace_         


        
7条回答
  •  囚心锁ツ
    2020-12-01 05:18

    Try os.path.splitext it should do what you want.

    import os
    print os.path.splitext('/home/user/somefile.txt')[0]+'.jpg'
    

提交回复
热议问题