如何从Python路径中获取不带扩展名的文件名? [英] How to get the filename without the extension from a path in Python?
本文介绍了如何从Python路径中获取不带扩展名的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从Python路径中获取不带扩展名的文件名?
How to get the filename without the extension from a path in Python?
推荐答案
获取不带扩展名的文件的名称:
Getting the name of the file without the extension:
import os
print(os.path.splitext("/path/to/some/file.txt")[0])
打印:
/path/to/some/file
os.path.splitext
的文档.
Documentation for os.path.splitext
.
重要说明:如果文件名包含多个点,则仅删除最后一个扩展名之后的扩展名.例如:
Important Note: If the filename has multiple dots, only the extension after the last one is removed. For example:
import os
print(os.path.splitext("/path/to/some/file.txt.zip.asc")[0])
打印:
/path/to/some/file.txt.zip
如果需要处理这种情况,请参见下面的其他答案.
See other answers below if you need to handle that case.
这篇关于如何从Python路径中获取不带扩展名的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文