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

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

python - 如何从Python路径中获取不带扩展名的文件名?(How to get the filename without the extension from a path in Python?)

如何从Python路径中获取不带扩展名的文件名?

  ask by Joan Venge translate from so

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

1 Answer

0 votes
by (71.8m points)

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.

(如果您需要处理这种情况,请参见下面的其他答案。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...