Getting the name of the file without the extension:
(获取不带扩展名的文件名:)
import os
print(os.path.splitext("/path/to/some/file.txt")[0])
Prints:
(印刷品:)
/path/to/some/file
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])
Prints:
(印刷品:)
/path/to/some/file.txt.zip
See other answers below if you need to handle that case.
(如果您需要处理这种情况,请参见下面的其他答案。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…