Benefits of os.path.splitext over regular .split?

前端 未结 11 975
独厮守ぢ
独厮守ぢ 2020-12-08 13:16

In this other question, the votes clearly show that the os.path.splitext function is preferred over the simple .split(\'.\')[-1] string manipulatio

11条回答
  •  长情又很酷
    2020-12-08 13:49

    splitext() does a reverse search for '.' and returns the extension portion as soon as it finds it. split('.') will do a forward search for all '.' characters and is therefore almost always slower. In other words splitext() is specifically written for returning the extension unlike split().

    (see posixpath.py in the Python source if you want to examine the implementation).

提交回复
热议问题