Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

是否有从文件名中提取扩展名的功能?

  ask by Alex translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
408 views
Welcome To Ask or Share your Answers For Others

1 Answer

Yes.

(是。)

Use os.path.splitext (see Python 2.X documentation or Python 3.X documentation ):

(使用os.path.splitext (请参阅Python 2.X文档Python 3.X文档 ):)

>>> import os
>>> filename, file_extension = os.path.splitext('/path/to/somefile.ext')
>>> filename
'/path/to/somefile'
>>> file_extension
'.ext'

Unlike most manual string-splitting attempts, os.path.splitext will correctly treat /a/bc/d as having no extension instead of having extension .c/d , and it will treat .bashrc as having no extension instead of having extension .bashrc :

(与大多数手动字符串拆分尝试不同, os.path.splitext/a/bc/d正确地视为不具有扩展名而不是扩展名.c/d ,并且将.bashrc视为不具有扩展名而不是具有extension .bashrc :)

>>> os.path.splitext('/a/b.c/d')
('/a/b.c/d', '')
>>> os.path.splitext('.bashrc')
('.bashrc', '')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...