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

前端 未结 23 1907
逝去的感伤
逝去的感伤 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:36

    Very very very simpely no other modules !!!

    import os
    p = r"C:\Users\bilal\Documents\face Recognition python\imgs\northon.jpg"
    
    # Get the filename only from the initial file path.
    filename = os.path.basename(p)
    
    # Use splitext() to get filename and extension separately.
    (file, ext) = os.path.splitext(filename)
    
    # Print outcome.
    print("Filename without extension =", file)
    print("Extension =", ext)
    

提交回复
热议问题