Extracting extension from filename in Python

后端 未结 24 2178
感情败类
感情败类 2020-11-22 13:23

Is there a function to extract the extension from a filename?

24条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 14:20

    You can find some great stuff in pathlib module (available in python 3.x).

    import pathlib
    x = pathlib.PurePosixPath("C:\\Path\\To\\File\\myfile.txt").suffix
    print(x)
    
    # Output 
    '.txt'
    

提交回复
热议问题