How to get the file extension from a filename in Python?
如何从Python中的文件名获取文件扩展名?
For example, I would like to get “.txt” from “file.txt”.
例如,我想从“ file.txt”中获取“ .txt”。
The Python code:
Python代码:
>>> name, ext = os.path.splitext('file.txt')
>>> name
'file'
>>> ext
'.txt'
Manual of os.path.splitext: https://docs.python.org/2/library/os.path.html#os.path.splitext
os.path.splitext手册: https : //docs.python.org/2/library/os.path.html#os.path.splitext
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。
翻译自: https://www.systutorials.com/how-to-get-the-file-extension-from-a-filename-in-python/