Skip to content

Speedup os.path.relpath() #117607

Closed
Closed
@nineteendo

Description

@nineteendo

Feature or enhancement

Proposal:

Currently both implemenatations of relpath() are a bit inefficient. So they could use some optimisations:

  1. We don't need to apply normpath() before abspath(), it already normalises it:
    -start_abs = abspath(normpath(start))
    -path_abs = abspath(normpath(path))
    +start_abs = abspath(start)
    +path_abs = abspath(path)
  2. We don't need to filter the segments, we just need to check if *_rest is empty:
    -start_list = [x for x in start_rest.split(sep) if x]
    -path_list = [x for x in path_rest.split(sep) if x]
    +start_list = start_rest.split(sep) if start_rest else []
    +path_list = path_rest.split(sep) if path_rest else []
  3. We can use str.join() instead of os.path.join():
    -return join(*rel_list)
    +return sep.join(rel_list)

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagestdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions