relpath

relpath(path::AbstractString, startpath::AbstractString = ".") -> AbstractString

Return a relative filepath to path either from the current directory or from an optional start directory. This is a path computation: the filesystem is not accessed to confirm the existence or nature of path or startpath.

Examples

In the Julia programming language, the function relpath(path::AbstractString, startpath::AbstractString = .) computes the relative filepath to path from either the current directory or an optional start directory. It does not access the filesystem to confirm the existence or nature of path or startpath. Here are some examples of its usage:

  1. Compute relative path from the current directory:

    julia> relpath("data/text.txt")
    "data/text.txt"

    This example computes the relative path from the current directory to the file "data/text.txt".

  2. Compute relative path from a specified start directory:

    julia> relpath("project/src/main.jl", "project")
    "src/main.jl"

    It computes the relative path from the "project" directory to the file "project/src/main.jl".

  3. Handle cases where the path is already relative:
    julia> relpath("../docs/index.html")
    "../docs/index.html"

    In this example, the function correctly handles the case where the path is already relative.

Common mistake example:

julia> relpath("/absolute/path", "start")
ERROR: MethodError: no method matching relpath(::String, ::String)

In this case, the function call produces an error because the relpath function does not accept absolute paths as arguments. Make sure to provide a relative path or a valid start directory to avoid such errors.

See Also

abspath, basename, chmod, countlines, cp, ctime, dirname, download, evalfile, expanduser, fdio, filemode, filesize, functionloc, gperm, homedir, include_string, isabspath, isblockdev, ischardev, isdir, isdirpath, isexecutable, isfifo, isfile, islink, ismount, ispath, isreadable, issetgid, issetuid, issticky, iswritable, joinpath, less, lstat, mkdir, mkpath, mktemp, mktempdir, mtime, mv, normpath, operm, poll_fd, poll_file, readall, readcsv, readdir, readdlm, readlines, readlink, realpath, relpath, rm, splitdir, splitdrive, splitext, stat, symlink, tempdir, tempname, touch, truncate, uperm, watch_file, writecsv,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: