How to get the filename without the extension from a path in Python?

前端 未结 23 2122
逝去的感伤
逝去的感伤 2020-11-22 05:43

How to get the filename without the extension from a path in Python?

For instance, if I had "/path/to/some/file.txt", I would want "

23条回答
  •  天命终不由人
    2020-11-22 06:50

    import os

    filename = C:\\Users\\Public\\Videos\\Sample Videos\\wildlife.wmv
    

    This returns the filename without the extension(C:\Users\Public\Videos\Sample Videos\wildlife)

    temp = os.path.splitext(filename)[0]  
    

    Now you can get just the filename from the temp with

    os.path.basename(temp)   #this returns just the filename (wildlife)
    

提交回复
热议问题